高精度加法
作者:
索隆
,
2021-11-05 14:53:47
,
所有人可见
,
阅读 274
#include<iostream>
#include<vector>
using namespace std;
vector<int> add(vector<int> &a, vector<int> &b){
vector<int> c;
int t = 0;
if(b.size() > a.size()) return add(b, a);
for(int i = 0; i < a.size() ; i ++ ){
t += a[i];
if(i < b.size()) t += b[i];
c.push_back(t % 10);
t /= 10;
}
if(t) c.push_back(1);
return c;
}
int main(){
string a, b;
vector<int> A, B;
cin >> a >> b;
for(int i = a.size() - 1; i >= 0; i -- ) A.push_back(a[i] - '0');
for(int i = b.size() - 1; i >= 0; i -- ) B.push_back(b[i] - '0');
auto c = add(A, B);
for(int i = c.size() - 1; i >= 0; i -- ) printf("%d", c[i]);
return 0;
}
#include<iostream>
#include<vector>
using namespace std;
vector<int> add(vector<int> &a, vector<int> &b){
vector<int> c;
int t = 0;
for(int i = 0; i < a.size() || i < b.size(); i ++ ){
if(i < a.size()) t += a[i];
if(i < b.size()) t += b[i];
c.push_back(t % 10);
t /= 10;
}
if(t) c.push_back(1);
return c;
}
int main(){
string a, b;
vector<int> A, B;
cin >> a >> b;
for(int i = a.size() - 1; i >= 0; i -- ) A.push_back(a[i] - '0');
for(int i = b.size() - 1; i >= 0; i -- ) B.push_back(b[i] - '0');
auto c = add(A, B);
for(int i = c.size() - 1; i >= 0; i -- ) printf("%d", c[i]);
return 0;
}