#include <iostream> #include <string> using namespace std; int main() { string a; string b; cin >> a; cin >> b; int m; int r = 0; int LA = a.length(); int LB = b.length(); string result = ""; if (LB > LA) { swap(a,b); swap(LA,LB); } int dif = LA - LB; for (int i=LA-1;i>=0;i--) { if (i>=dif) { m = int(a[i])+ int(b[i-dif])-96 + r; result.push_back(char(m%10 + 48)); if (m>9) { r = 1; } else { r = 0; } } else { if (r == 1) { if (a[i] == '9') { result.push_back('0'); } else { r = 0; result.push_back(a[i]+1); } } else { result.push_back(a[i]); } } } if (r==1) { result.push_back('1'); LA++; } for (int i=0;i < LA/2;i++) { swap(result[i],result[LA-i-1]); } cout << result; }
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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | #include <iostream> #include <string> using namespace std; int main() { string a; string b; cin >> a; cin >> b; int m; int r = 0; int LA = a.length(); int LB = b.length(); string result = ""; if (LB > LA) { swap(a,b); swap(LA,LB); } int dif = LA - LB; for (int i=LA-1;i>=0;i--) { if (i>=dif) { m = int(a[i])+ int(b[i-dif])-96 + r; result.push_back(char(m%10 + 48)); if (m>9) { r = 1; } else { r = 0; } } else { if (r == 1) { if (a[i] == '9') { result.push_back('0'); } else { r = 0; result.push_back(a[i]+1); } } else { result.push_back(a[i]); } } } if (r==1) { result.push_back('1'); LA++; } for (int i=0;i < LA/2;i++) { swap(result[i],result[LA-i-1]); } cout << result; } |