#include <iostream> #include <string> using namespace std; int main() { int i, j, n, res, dig, to_add; string a, b; cin >> a; cin >> b; n = max(a.size(), b.size()) + 1; int sum[n]{0}; for (i = 0; i < n; i++) sum[i + 1] = a[i] - '0'; i = n - 1; j = b.size() - 1; res = 0; do { dig = b[j] - '0'; to_add = sum[i] + dig + res; sum[i] = (to_add) % 10; res = (to_add) / 10; i--; j--; } while (j >= 0); while (sum[i] + res > 9) { to_add = sum[i] + res; sum[i] = (sum[i] + res) % 10; res = to_add / 10; i--; } sum[i] = sum[i] + res; for (i = 0; i < n; i++) if (i == 0 && sum[i] == 0) continue; else cout << sum[i]; 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 36 37 38 39 40 41 42 | #include <iostream> #include <string> using namespace std; int main() { int i, j, n, res, dig, to_add; string a, b; cin >> a; cin >> b; n = max(a.size(), b.size()) + 1; int sum[n]{0}; for (i = 0; i < n; i++) sum[i + 1] = a[i] - '0'; i = n - 1; j = b.size() - 1; res = 0; do { dig = b[j] - '0'; to_add = sum[i] + dig + res; sum[i] = (to_add) % 10; res = (to_add) / 10; i--; j--; } while (j >= 0); while (sum[i] + res > 9) { to_add = sum[i] + res; sum[i] = (sum[i] + res) % 10; res = to_add / 10; i--; } sum[i] = sum[i] + res; for (i = 0; i < n; i++) if (i == 0 && sum[i] == 0) continue; else cout << sum[i]; return 0; } |