#include <bits/stdc++.h> using namespace std; int k, l, p, mx, ax, bx, suma[5007], c[5007]; int main() { ios_base::sync_with_stdio(false); cin.tie(); string a, b; cin >> a >> b; k = a.size(); l = b.size(); mx = max(k, l); if (l > k) { for (int i = l-1; i >= 0; --i) { c[i+l-k] = a[i]; if (i < (l - k)) c[i] = 48; } } else if (k > l) { for (int i = k-1; i >= 0; --i) { c[i+k-l] = b[i]; if (i < (k - l)) c[i] = 48; } } for (int i = mx-1; i >= 0; --i) { if (l > k) { ax = c[i] - 48; bx = b[i] - 48; } else if (k > l) { ax = a[i] - 48; bx = c[i] - 48; } else { ax = a[i] - 48; bx = b[i] - 48; } if ((ax + bx + p) < 10) { suma[i] = ax + bx + p; p = 0; } else { suma[i] = ((ax + bx + p)%10); p = (ax + bx + p)/10; } } if (p) cout << p; for (int i = 0; i <= mx-1; i++) cout << suma[i]; cout << "\n"; }
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 48 49 50 51 52 | #include <bits/stdc++.h> using namespace std; int k, l, p, mx, ax, bx, suma[5007], c[5007]; int main() { ios_base::sync_with_stdio(false); cin.tie(); string a, b; cin >> a >> b; k = a.size(); l = b.size(); mx = max(k, l); if (l > k) { for (int i = l-1; i >= 0; --i) { c[i+l-k] = a[i]; if (i < (l - k)) c[i] = 48; } } else if (k > l) { for (int i = k-1; i >= 0; --i) { c[i+k-l] = b[i]; if (i < (k - l)) c[i] = 48; } } for (int i = mx-1; i >= 0; --i) { if (l > k) { ax = c[i] - 48; bx = b[i] - 48; } else if (k > l) { ax = a[i] - 48; bx = c[i] - 48; } else { ax = a[i] - 48; bx = b[i] - 48; } if ((ax + bx + p) < 10) { suma[i] = ax + bx + p; p = 0; } else { suma[i] = ((ax + bx + p)%10); p = (ax + bx + p)/10; } } if (p) cout << p; for (int i = 0; i <= mx-1; i++) cout << suma[i]; cout << "\n"; } |