Files
Data-Structure/Algorithm/BigNum/P1601 A+B Problem(高精).cpp
2025-09-21 12:21:50 +08:00

36 lines
733 B
C++

#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
string add(string a, string b) {
reverse(a.begin(), a.end());
reverse(b.begin(), b.end());
int ms = max(a.size(), b.size());
string tmp;
tmp.resize(ms + 1, '0');
int carry = 0;
for (int i = 0; i < ms; i++) {
int x = (i < a.size() ? a[i] - '0' : 0);
int y = (i < b.size() ? b[i] - '0' : 0);
int sum = x + y + carry;
tmp[i] = sum % 10 + '0';
carry = sum / 10;
}
if (carry) tmp[ms] = carry + '0';
else tmp.pop_back();
reverse(tmp.begin(), tmp.end());
return tmp;
}
int main() {
string a, b;
cin >> a >> b;
cout << add(a, b) << endl;
return 0;
}