#include <bits/stdc++.h> using namespace std; int main() { string a, b, c; c = ""; cin >> a >> b; if (a.size() < b.size()) { swap(a,b); } if (a.size() > b.size()) { string p = ""; for (int i = 0; i < a.size()-b.size(); i++) { p += '0'; } b = p + b; } bool prz = 0; for (int i = a.size()-1; i >= 0; i--) { if (prz) { if ((a[i]-'0') + (b[i]-'0')+1 > 9) { c += char(a[i] + b[i] - '0' - 9); prz = 1; } else { c += char(a[i] + b[i] - '0' + 1); prz = 0; } } else { if ((a[i]-'0') + (b[i]-'0') > 9) { c += char(a[i] + b[i] - '0' - 10); prz = 1; } else { c += char(a[i] + b[i] - '0'); prz = 0; } } } if (prz) { cout << 1; } for (int i = c.size()-1; i >= 0; i--) { cout << c[i]; } }
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 | #include <bits/stdc++.h> using namespace std; int main() { string a, b, c; c = ""; cin >> a >> b; if (a.size() < b.size()) { swap(a,b); } if (a.size() > b.size()) { string p = ""; for (int i = 0; i < a.size()-b.size(); i++) { p += '0'; } b = p + b; } bool prz = 0; for (int i = a.size()-1; i >= 0; i--) { if (prz) { if ((a[i]-'0') + (b[i]-'0')+1 > 9) { c += char(a[i] + b[i] - '0' - 9); prz = 1; } else { c += char(a[i] + b[i] - '0' + 1); prz = 0; } } else { if ((a[i]-'0') + (b[i]-'0') > 9) { c += char(a[i] + b[i] - '0' - 10); prz = 1; } else { c += char(a[i] + b[i] - '0'); prz = 0; } } } if (prz) { cout << 1; } for (int i = c.size()-1; i >= 0; i--) { cout << c[i]; } } |