#include <cstdio> #include <cstring> char a[5002], b[5002], ret[5002]; int main() { scanf("%s %s", a, b); int n = strlen(a), m = strlen(b); for (int i = 0; i < n; i++) { a[5000 - i] = a[n - i - 1]; } for (int i = 0; i <= 5000 - n; i++) { a[i] = '0'; } for (int i = 0; i < m; i++) { b[5000 - i] = b[m - i - 1]; } for (int i = 0; i <= 5000 - m; i++) { b[i] = '0'; } int c = 0; for (int i = 5000; i >= 0; i--) { int x = a[i] - '0'; int y = b[i] - '0'; int r = x + y + c; ret[i] = r % 10 + '0'; c = r > 9 ? 1 : 0; } c = 0; while (ret[c] == '0') c++; printf("%s", ret + c); 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 | #include <cstdio> #include <cstring> char a[5002], b[5002], ret[5002]; int main() { scanf("%s %s", a, b); int n = strlen(a), m = strlen(b); for (int i = 0; i < n; i++) { a[5000 - i] = a[n - i - 1]; } for (int i = 0; i <= 5000 - n; i++) { a[i] = '0'; } for (int i = 0; i < m; i++) { b[5000 - i] = b[m - i - 1]; } for (int i = 0; i <= 5000 - m; i++) { b[i] = '0'; } int c = 0; for (int i = 5000; i >= 0; i--) { int x = a[i] - '0'; int y = b[i] - '0'; int r = x + y + c; ret[i] = r % 10 + '0'; c = r > 9 ? 1 : 0; } c = 0; while (ret[c] == '0') c++; printf("%s", ret + c); return 0; } |