#include <iostream> #include <string> using namespace std; int main() { string a, b, c = ""; cin >> a >> b; int t = 0; for (int i = 0; i < max(a.length(), b.length()); i++) { t += ( (i < a.length()) ? (int)a[a.length()-1-i] - 48 : 0 ); t += ( (i < b.length()) ? (int)b[b.length()-1-i] - 48 : 0 ); c += (char)( (t % 10) + 48); t /= 10; } if (t != 0) { c += t; } for (int i = c.length() - 1; i > -1; i--) { cout << c[i]; } cout << endl; }
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 | #include <iostream> #include <string> using namespace std; int main() { string a, b, c = ""; cin >> a >> b; int t = 0; for (int i = 0; i < max(a.length(), b.length()); i++) { t += ( (i < a.length()) ? (int)a[a.length()-1-i] - 48 : 0 ); t += ( (i < b.length()) ? (int)b[b.length()-1-i] - 48 : 0 ); c += (char)( (t % 10) + 48); t /= 10; } if (t != 0) { c += t; } for (int i = c.length() - 1; i > -1; i--) { cout << c[i]; } cout << endl; } |