#include <iostream> #include <string> #include <sstream> using namespace std; int main() { string a, b; cin >> a >> b; stringstream s; for (int i = a.size() - 1; i >= 0; i--) { s << a[i]; } a = s.str(); s.str(""); for (int i = b.size() - 1; i >= 0; i--) { s << b[i]; } b = s.str(); stringstream sv; int fl = 0, hl; int mxm = (a.size() >= b.size() ? a.size() : b.size()); for (int i = 0; i < mxm; i++) { if (a.size() > i && b.size() > i) { hl = (char)a[i] - 48 + (char)b[i] - 48; } else { hl = (a.size() > b.size() ? (char)a[i] - 48 : (char)b[i] - 48); } hl += fl; if (hl > 9) { fl = 1; sv << hl % 10; } else { sv << hl; fl = 0; } } if (fl) { sv << fl; } string otp = sv.str(); for (int i = otp.size() - 1; i >= 0; i--) { cout << otp[i]; } cout << endl; 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 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 68 69 70 71 72 73 74 75 | #include <iostream> #include <string> #include <sstream> using namespace std; int main() { string a, b; cin >> a >> b; stringstream s; for (int i = a.size() - 1; i >= 0; i--) { s << a[i]; } a = s.str(); s.str(""); for (int i = b.size() - 1; i >= 0; i--) { s << b[i]; } b = s.str(); stringstream sv; int fl = 0, hl; int mxm = (a.size() >= b.size() ? a.size() : b.size()); for (int i = 0; i < mxm; i++) { if (a.size() > i && b.size() > i) { hl = (char)a[i] - 48 + (char)b[i] - 48; } else { hl = (a.size() > b.size() ? (char)a[i] - 48 : (char)b[i] - 48); } hl += fl; if (hl > 9) { fl = 1; sv << hl % 10; } else { sv << hl; fl = 0; } } if (fl) { sv << fl; } string otp = sv.str(); for (int i = otp.size() - 1; i >= 0; i--) { cout << otp[i]; } cout << endl; return 0; } |