#include<bits/stdc++.h> using namespace std; string a, b; vector <int> res; int N, temp; int main() { cin.sync_with_stdio(0); cout.sync_with_stdio(0); cin >> a >> b; N = max(a.size(), b.size()); if(a.size() < b.size()) swap(a, b); if(a.size() != b.size()) { reverse(b.begin(), b.end()); int x = b.size(); for(int i = 0; i < N - x; ++i) b.push_back('0'); reverse(b.begin(), b.end()); } for(int i = N - 1; i >= 0; --i) res.push_back((a[i] - 48) + (b[i] - 48)); reverse(res.begin(), res.end()); for(int i = res.size() - 1; i >= 0; --i) { res[i] += temp; if(res[i] > 9) temp = res[i] / 10; else temp = 0; res[i] %= 10; } if(temp != 0) cout << temp; //cout << a << "\n" << b << "\n"; for(int i = 0; i < res.size(); ++i) cout << res[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 | #include<bits/stdc++.h> using namespace std; string a, b; vector <int> res; int N, temp; int main() { cin.sync_with_stdio(0); cout.sync_with_stdio(0); cin >> a >> b; N = max(a.size(), b.size()); if(a.size() < b.size()) swap(a, b); if(a.size() != b.size()) { reverse(b.begin(), b.end()); int x = b.size(); for(int i = 0; i < N - x; ++i) b.push_back('0'); reverse(b.begin(), b.end()); } for(int i = N - 1; i >= 0; --i) res.push_back((a[i] - 48) + (b[i] - 48)); reverse(res.begin(), res.end()); for(int i = res.size() - 1; i >= 0; --i) { res[i] += temp; if(res[i] > 9) temp = res[i] / 10; else temp = 0; res[i] %= 10; } if(temp != 0) cout << temp; //cout << a << "\n" << b << "\n"; for(int i = 0; i < res.size(); ++i) cout << res[i]; } |