#include <iostream> #include <string> using namespace std; int main() { string a, b; cin >> a >> b; if (a.length() < b.length()) a = string(b.length() - a.length(), '0') + a; else if (b.length() < a.length()) b = string(a.length() - b.length(), '0') + b; string res; int carry = 0; for (int i = a.length() - 1; i >= 0; --i) { int sum = (a[i] - '0') + (b[i] - '0') + carry; res = to_string(sum % 10) + res; carry = sum / 10; } if (carry > 0) res = to_string(carry) + res; cout << res << endl; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | #include <iostream> #include <string> using namespace std; int main() { string a, b; cin >> a >> b; if (a.length() < b.length()) a = string(b.length() - a.length(), '0') + a; else if (b.length() < a.length()) b = string(a.length() - b.length(), '0') + b; string res; int carry = 0; for (int i = a.length() - 1; i >= 0; --i) { int sum = (a[i] - '0') + (b[i] - '0') + carry; res = to_string(sum % 10) + res; carry = sum / 10; } if (carry > 0) res = to_string(carry) + res; cout << res << endl; return 0; } |