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
| #include <iostream> #include <string> using namespace std; #define N 520 int a[N], b[N], c[N] = { 0 }; int main() { string A, B; cin >> A >> B; int len = A.length() > B.length() ? A.length() : B.length(); for (int i = A.length() - 1, j = 0; i >= 0; i--, j++) { a[j] = A[i] - '0'; } for (int i = B.length() - 1, j = 0; i >= 0; i--, j++) { b[j] = B[i] - '0'; } for (int i = 0; i <= len - 1; i++) { c[i] += a[i] + b[i]; c[i + 1] += c[i] / 10; c[i] %= 10; } if (c[len])len++; for (int i = len - 1; i >= 0; i--) { cout << c[i]; } cout << endl; return 0; }
|