题解 | #高精度整数加法#
高精度整数加法
https://www.nowcoder.com/practice/49e772ab08994a96980f9618892e55b6
#include <iostream> #include <string> #include <algorithm> using namespace std; int main() { string a, b; cin >> a >> b; string result; if (a.size() > b.size()) swap(a, b); int len = b.size(); string pad(len - a.size(), '0'); a = pad + a; int asd = 0; for (int i = 1; i <= len; ++i) { int num = a[len - i] - '0' + b[len - i] - '0'; num += asd; asd = num > 9 ? 1 : 0; num %= 10; result += to_string(num); } if (asd == 1) result += '1'; reverse(result.begin(), result.end()); cout << result << endl; } // 64 位输出请用 printf("%lld")