// // main.cpp // A+B // // Created by Mikołaj Jędrzejewski on 12/8/22. // #include <iostream> #include <vector> #include <stack> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); char d; vector<int> a, b; while (cin.get(d)) { if (d == '\n') break; a.push_back(d-'0'); } while (cin.get(d)) { if (d == '\n') break; b.push_back(d-'0'); } int ali = a.size()-1, bli = b.size()-1; // last index stack<int> s; int r = 0; int li = min(ali, bli); int i = 0; while (i<=li) { int c = a[ali-i] + b[bli-i] + r; int p = c%10; s.push(p); (c > 9)? r = 1 : r = 0; ++i; } while (i<=ali) { int c = a[ali-i] + r; int p = c%10; s.push(p); (c > 9)? r = 1 : r = 0; ++i; } while (i<=bli) { int c = b[bli-i] + r; int p = c%10; s.push(p); (c > 9)? r = 1 : r = 0; ++i; } if (r == 1) s.push(r); while (!s.empty()) { cout<<s.top(); s.pop(); } 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 | // // main.cpp // A+B // // Created by Mikołaj Jędrzejewski on 12/8/22. // #include <iostream> #include <vector> #include <stack> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); char d; vector<int> a, b; while (cin.get(d)) { if (d == '\n') break; a.push_back(d-'0'); } while (cin.get(d)) { if (d == '\n') break; b.push_back(d-'0'); } int ali = a.size()-1, bli = b.size()-1; // last index stack<int> s; int r = 0; int li = min(ali, bli); int i = 0; while (i<=li) { int c = a[ali-i] + b[bli-i] + r; int p = c%10; s.push(p); (c > 9)? r = 1 : r = 0; ++i; } while (i<=ali) { int c = a[ali-i] + r; int p = c%10; s.push(p); (c > 9)? r = 1 : r = 0; ++i; } while (i<=bli) { int c = b[bli-i] + r; int p = c%10; s.push(p); (c > 9)? r = 1 : r = 0; ++i; } if (r == 1) s.push(r); while (!s.empty()) { cout<<s.top(); s.pop(); } return 0; } |