#include<iostream> #include<vector> #include<stack> using namespace std; int main() { ios_base::sync_with_stdio(0); string A, B; stack<char> S1,S2; stack<int> R; cin >> A >> B; for (int i = 0; i < A.length(); i++) S1.push(A[i]); for (int i = 0; i < B.length(); i++) S2.push(B[i]); int reszta = 0; while (!S1.empty() && !S2.empty()) { int result = S1.top()-'0' + S2.top()-'0' + reszta; reszta = 0; S1.pop();S2.pop(); if (result >= 10) { result -= 10; reszta++; } R.push(result); } stack<char> H; if (!S1.empty()) H = S1; else H = S2; while (!H.empty()) { int result = H.top()-'0' + reszta; reszta = 0; H.pop(); if (result >= 10 && !H.empty()) { result -= 10; reszta++; } R.push(result); } if (reszta) R.push(reszta); while (!R.empty()) { cout << R.top(); R.pop(); } 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 | #include<iostream> #include<vector> #include<stack> using namespace std; int main() { ios_base::sync_with_stdio(0); string A, B; stack<char> S1,S2; stack<int> R; cin >> A >> B; for (int i = 0; i < A.length(); i++) S1.push(A[i]); for (int i = 0; i < B.length(); i++) S2.push(B[i]); int reszta = 0; while (!S1.empty() && !S2.empty()) { int result = S1.top()-'0' + S2.top()-'0' + reszta; reszta = 0; S1.pop();S2.pop(); if (result >= 10) { result -= 10; reszta++; } R.push(result); } stack<char> H; if (!S1.empty()) H = S1; else H = S2; while (!H.empty()) { int result = H.top()-'0' + reszta; reszta = 0; H.pop(); if (result >= 10 && !H.empty()) { result -= 10; reszta++; } R.push(result); } if (reszta) R.push(reszta); while (!R.empty()) { cout << R.top(); R.pop(); } cout << endl; return 0; } |