#include <bits/stdc++.h> using namespace std; string add(string a, string b) { string c=""; reverse(a.begin(), a.end()); reverse(b.begin(), b.end()); if(a.length()<b.length()) swap(a, b); while(b.length()<a.length()) b+='0'; while(c.length()<a.length()) c+='0'; c+='0'; int r=0; int n=a.length(); for(int i=0; i<n; ++i) { c[i]=((a[i]-48+b[i]-48+r)%10)+48; r=(a[i]-48+b[i]-48+r)/10; } if(r) c[n]=r+48; else c.erase(c.end()-1); reverse(c.begin(), c.end()); return c; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); string a, b; cin>>a>>b; cout<<add(a, b)<<"\n"; 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 | #include <bits/stdc++.h> using namespace std; string add(string a, string b) { string c=""; reverse(a.begin(), a.end()); reverse(b.begin(), b.end()); if(a.length()<b.length()) swap(a, b); while(b.length()<a.length()) b+='0'; while(c.length()<a.length()) c+='0'; c+='0'; int r=0; int n=a.length(); for(int i=0; i<n; ++i) { c[i]=((a[i]-48+b[i]-48+r)%10)+48; r=(a[i]-48+b[i]-48+r)/10; } if(r) c[n]=r+48; else c.erase(c.end()-1); reverse(c.begin(), c.end()); return c; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); string a, b; cin>>a>>b; cout<<add(a, b)<<"\n"; return 0; } |