#include <iostream> using namespace std; int main () { string a, b; cin >> a >> b; int i, j, carry{}; if (a.size() < b.size()) swap (a, b); for (i = a.size()-1, j = b.size()-1; j >= 0; --i, --j) { a[i] += carry + b[j] - '0'; if (a[i] > '9') a[i] -= 10, carry = 1; else carry = 0; } while (carry and i >= 0) { a[i] += carry; if (a[i] > '9') a[i] -= 10; else carry = 0; --i; } if (carry) cout << 1; cout << a << endl; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | #include <iostream> using namespace std; int main () { string a, b; cin >> a >> b; int i, j, carry{}; if (a.size() < b.size()) swap (a, b); for (i = a.size()-1, j = b.size()-1; j >= 0; --i, --j) { a[i] += carry + b[j] - '0'; if (a[i] > '9') a[i] -= 10, carry = 1; else carry = 0; } while (carry and i >= 0) { a[i] += carry; if (a[i] > '9') a[i] -= 10; else carry = 0; --i; } if (carry) cout << 1; cout << a << endl; } |