#include<iostream> #include<algorithm> #include<string> using namespace std; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); string a, b; string result; int sum; int next = 0; cin >> a >> b; if (a.length() > b.length()) { b = string(a.length() - b.length(), '0') + b; } else if (a.length() < b.length()) { a = string(b.length() - a.length(), '0') + a; } // -48 for (int i = a.length() - 1; i >= 0; --i) { sum = a[i] + b[i] - 96 + next; next = sum / 10; result.push_back((sum % 10) + 48); } reverse(result.begin(), result.end()); if (next > 0) { cout << to_string(next) + result; } else { cout << result; } }
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | #include<iostream> #include<algorithm> #include<string> using namespace std; int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); string a, b; string result; int sum; int next = 0; cin >> a >> b; if (a.length() > b.length()) { b = string(a.length() - b.length(), '0') + b; } else if (a.length() < b.length()) { a = string(b.length() - a.length(), '0') + a; } // -48 for (int i = a.length() - 1; i >= 0; --i) { sum = a[i] + b[i] - 96 + next; next = sum / 10; result.push_back((sum % 10) + 48); } reverse(result.begin(), result.end()); if (next > 0) { cout << to_string(next) + result; } else { cout << result; } } |