• 微信公众号：美女很有趣。 工作之余，放松一下，关注即送10G+美女照片！

# 高精度运算

2周前 (05-03) 8次浏览

## 输入输出样例

``````1
1
``````

``````2
``````

``````1001
9099
``````

``````10100
``````

``````string add(string str1,string str2)//高精度加法
{
string str;
int len1=str1.length();
int len2=str2.length();
//前面补0，弄成长度相同
if(len1<len2)
{
for(int i=1;i<=len2-len1;i++)
str1="0"+str1;
}
else
{
for(int i=1;i<=len1-len2;i++)
str2="0"+str2;
}
len1=str1.length();
int cf=0;
int temp;
for(int i=len1-1;i>=0;i--)
{
temp=str1[i]-'0'+str2[i]-'0'+cf;
cf=temp/10;
temp%=10;
str=char(temp+'0')+str;
}
if(cf!=0)  str=char(cf+'0')+str;
return str;
}
``````

``````#include<bits/stdc++.h> //万能头文件
using namespace std;
int main () {
string a;
string b;
cin>>a;
cin>>b;
int i = a.length() - 1;
int j = b.length() - 1;
int yu = 0;
int count = 0;
vector<int> v;
while (i >= 0 && j >= 0) {
int one = a[i] - '0';
int two = b[j] - '0';
count = one + two + yu;
yu = count / 10;
v.push_back(count % 10);
i--;
j--;
}
if (i == j && yu != 0) {
v.push_back(yu);
} else {
while (i >= 0) {
count = a[i] - '0' + yu;
yu = count / 10;
v.push_back(count % 10);
i--;
}
while (j >= 0) {
count = b[j] - '0' + yu;
yu = count / 10;
v.push_back(count % 10);
j--;
}
}
if (yu != 0) {
v.push_back(yu);
}
for (int i = v.size() - 1; i >= 0; i--) {
cout<<v[i];
}

}
``````

## 输入输出样例

``````1
2
``````

``````2
``````

## 说明/提示

``````#include<bits/stdc++.h> //万能头文件
using namespace std;

char a1[50001], b1[50001];
int a[50001], b[50001], i, x, len, j, c[50001];
int main () {
//读入两个数
cin>>a1>>b1;
//计算长度
a[0] = strlen(a1);
b[0] = strlen(b1);
for (i = 1; i <= a[0]; i++) {
//将字符串转成为数字，并且反转
a[i] = a1[a[0] - i] - '0';
}
for (i = 1; i <= b[0]; ++i) {
//将字符串转成为数字，并且反转
b[i] = b1[b[0] - i] - '0';
}
//    for (i = 1; i < a[0]; i++) {
//        cout<<a[i];
//    }
//    cout<<endl;
//    for (i = 1; i < b[0]; i++) {
//        cout<<b[i];
//    }

//假设a1 = "876"
//b1 = "987"
//此时a数组为 6 7 8
//b数组为7 8 9
for (i = 1; i <= a[0]; i++) {
for (j = 1; j <= b[0]; j++) {
//错位进行相乘，这个时候还不进行进位
//此过程模拟的就是，乘法过程，并且将每次的结果保存到c数组中
c[i + j - 1] += a[i] * b[j];
}
}
len = a[0] + b[0];
for (i = 1; i < len; i++) {
//此时要进行进位操作
//根据数组保存的性质，在结果集中c[i] 是结果的个位，百位...数，往前推
if (c[i] > 9) {
c[i + 1] += c[i] / 10;
c[i] %= 10;
}
}
//判断位数 例如10 * 0 会出现00的情况
while (c[len] == 0 && len > 1) {
len--;
}
for (int i = len; i >= 1; i--) {
cout<<c[i];//输出
}
return 0;

}
``````

``````#include<bits/stdc++.h> //万能头文件
using namespace std;

int main () {
string a;
string b;
cin>>a;
cin>>b;
int yu = 0;
vector<vector<int> > v;
int k = 0;
for (int i = b.size() - 1; i >= 0; i--) {
vector<int> temp;
int j = a.size() - 1;
int yu = 0;
int m = k;
while (m > 0) {
//cout<<0;
temp.push_back(0);
m--;
}
k++;
while (j >= 0) {
int one = b[i] - '0';
int two = a[j] - '0';
temp.push_back((one * two + yu) % 10);
yu = (one * two + yu) / 10;
j--;
}
if (yu != 0) {
temp.push_back(yu);
}
v.push_back(temp);
}
vector<int> ans;
for (int i = 0; i < v.size(); i++) {
//cout<<v[i].size()<<endl;
ans.push_back(v[i].size());
}
int max_value = *max_element(ans.begin(), ans.end());
vector<int> re;
yu = 0;
int count = 0;
for (int j = 0; j < max_value; j++) {
count = 0;
for (int i = 0; i < b.size(); i++) {
if (j < ans[i]) {
count += v[i][j];
}
}
re.push_back((count + yu) % 10);
yu = (count + yu) / 10;
}
int i;
for (i = re.size() - 1; i > 0; i--) {
if (re[i] != 0) {
break;
}
}
for (int j = i; j >= 0; j--) {
cout<<re[j];
}

}
``````