#include <bits/stdc++.h> using namespace std; int main(){ string a, b; cin >> a >> b; int n = a.length(); int m = b.length(); if(n < m){ swap(a, b); swap(n, m); } string add; for(int i = 0; i<n-m; ++i){ add += '0'; } b = add + b; stack <int> s; int rest = 0; for(int i = n-1; i>=0; --i){ int x = a[i]-'0'+b[i]-'0'+rest; s.push(x%10); rest = x/10; } if(rest) cout << rest; while(!s.empty()){ cout << s.top(); s.pop(); } }
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 | #include <bits/stdc++.h> using namespace std; int main(){ string a, b; cin >> a >> b; int n = a.length(); int m = b.length(); if(n < m){ swap(a, b); swap(n, m); } string add; for(int i = 0; i<n-m; ++i){ add += '0'; } b = add + b; stack <int> s; int rest = 0; for(int i = n-1; i>=0; --i){ int x = a[i]-'0'+b[i]-'0'+rest; s.push(x%10); rest = x/10; } if(rest) cout << rest; while(!s.empty()){ cout << s.top(); s.pop(); } } |