题解 | #高精度整数加法#
高精度整数加法
https://www.nowcoder.com/practice/49e772ab08994a96980f9618892e55b6
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
string a;
string b;
cin >> a;
cin >> b;
int i = a.length() - 1;
int j = b.length() - 1;
vector<int> res;
int carry = 0;
char up;
char down;
while(i >= 0 || j >= 0) {
if(i >= 0)
up = a[i];
else
up = '0';
if(j >= 0)
down = b[j];
else
down = '0';
int t = up + down - '0' - '0' + carry;
res.push_back(t % 10);
carry = t / 10;
i--;
j--;
}
if(carry) res.push_back(carry);
reverse(res.begin(), res.end());
for(auto& i: res)
cout << i;
cout << endl;
return 0;
}
// 64 位输出请用 printf("%lld")
竖式加法模拟即可。
