#include<iostream> #include<string> using namespace std; int main() { string a, b; cin >> a >> b; if (a.size() < b.size()) { int n = b.size() - a.size(); for (int i = 0; i < n; i++) { a = '0' + a; } } if (b.size() < a.size()) { int n = a.size() - b.size(); for (int i = 0; i < n; i++) { b = '0' + b; } } int carry = 0; string res = ""; for (int i = a.size() - 1; i >= 0; i--){ int v = (a[i] - '0') + (b[i] - '0') + carry; carry = v / 10; res = char(v % 10 + '0') + res; } if (carry) { res = '1' + 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 34 35 | #include<iostream> #include<string> using namespace std; int main() { string a, b; cin >> a >> b; if (a.size() < b.size()) { int n = b.size() - a.size(); for (int i = 0; i < n; i++) { a = '0' + a; } } if (b.size() < a.size()) { int n = a.size() - b.size(); for (int i = 0; i < n; i++) { b = '0' + b; } } int carry = 0; string res = ""; for (int i = a.size() - 1; i >= 0; i--){ int v = (a[i] - '0') + (b[i] - '0') + carry; carry = v / 10; res = char(v % 10 + '0') + res; } if (carry) { res = '1' + res; } cout << res << endl; return 0; } |