#include <cstdio> #include <vector> #include <algorithm> using namespace std; bool carry_out; vector<int> a, b, r; int c; int main(){ c = getchar(); while(c != '\n'){ a.push_back(c - '0'); c = getchar(); } c = getchar(); while(c != '\n'){ b.push_back(c - '0'); c = getchar(); } while(!a.empty() && !b.empty()){ r.push_back((a.back() + b.back() + carry_out) % 10); carry_out = (a.back() + b.back() + carry_out) >= 10; a.pop_back(); b.pop_back(); } while(!a.empty()){ r.push_back((a.back() + carry_out) % 10); carry_out = (a.back() + carry_out) >= 10; a.pop_back(); } while(!b.empty()){ r.push_back((b.back() + carry_out) % 10); carry_out = (b.back() + carry_out) >= 10; b.pop_back(); } if(carry_out) r.push_back(1); reverse(r.begin(), r.end()); for(auto i : r) printf("%d", i); }
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 | #include <cstdio> #include <vector> #include <algorithm> using namespace std; bool carry_out; vector<int> a, b, r; int c; int main(){ c = getchar(); while(c != '\n'){ a.push_back(c - '0'); c = getchar(); } c = getchar(); while(c != '\n'){ b.push_back(c - '0'); c = getchar(); } while(!a.empty() && !b.empty()){ r.push_back((a.back() + b.back() + carry_out) % 10); carry_out = (a.back() + b.back() + carry_out) >= 10; a.pop_back(); b.pop_back(); } while(!a.empty()){ r.push_back((a.back() + carry_out) % 10); carry_out = (a.back() + carry_out) >= 10; a.pop_back(); } while(!b.empty()){ r.push_back((b.back() + carry_out) % 10); carry_out = (b.back() + carry_out) >= 10; b.pop_back(); } if(carry_out) r.push_back(1); reverse(r.begin(), r.end()); for(auto i : r) printf("%d", i); } |