直接在字符串上操作即可。倒序方便进位。
#include <algorithm>
#include <iostream>
using namespace std;
string a;
string b;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> a >> b;
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
for (int i = 0; i < a.size() && i < b.size(); i++) {
if (i == a.size()) {
a += '0';
}
a[i] += i == b.size() ? 0 : b[i] - '0';
if (a[i] > '0' + 9) {
a[i] -= 10;
if (i + 1 == a.size()) {
a += '1';
} else {
a[i + 1]++;
}
}
}
reverse(a.begin(), a.end());
cout << a;
return 0;
}
// 64 位输出请用 printf("%lld")

京公网安备 11010502036488号