#include <iostream> #include <string> #include <algorithm> using namespace std; int main() { string a, b; cin >> a >> b; if (a.length() > b.length()) { swap(a, b); } int lenDiff = b.length() - a.length(); for (int i = 0; i < lenDiff; i++) { a = '0' + a; } string result = ""; int carry = 0; for (int i = a.length() - 1; i >= 0; i--) { int sum = (a[i] - '0') + (b[i] - '0') + carry; result = to_string(sum % 10) + result; carry = sum / 10; } if (carry > 0) { result = to_string(carry) + result; } cout << result << 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> #include <algorithm> using namespace std; int main() { string a, b; cin >> a >> b; if (a.length() > b.length()) { swap(a, b); } int lenDiff = b.length() - a.length(); for (int i = 0; i < lenDiff; i++) { a = '0' + a; } string result = ""; int carry = 0; for (int i = a.length() - 1; i >= 0; i--) { int sum = (a[i] - '0') + (b[i] - '0') + carry; result = to_string(sum % 10) + result; carry = sum / 10; } if (carry > 0) { result = to_string(carry) + result; } cout << result << endl; return 0; } |