#include <iostream> using namespace std; int main(){ string n, k; cin>>n>>k; string out; string tmp; if(n.size()<k.size()){ tmp = n; n = k; k = tmp; } int tmpsum; int carry = 0; int cn=n.size(), ck = k.size(); for(int i = 0; i<n.size(); i++){ cn--; ck--; tmpsum = n[cn]-'0'; if(ck>=0) tmpsum+=k[ck]-'0'; tmpsum+=carry; carry = tmpsum/10; out = (char)(tmpsum%10+'0')+out; } if(carry>0) out = (char)(carry+'0') + out; cout<<out; 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 | #include <iostream> using namespace std; int main(){ string n, k; cin>>n>>k; string out; string tmp; if(n.size()<k.size()){ tmp = n; n = k; k = tmp; } int tmpsum; int carry = 0; int cn=n.size(), ck = k.size(); for(int i = 0; i<n.size(); i++){ cn--; ck--; tmpsum = n[cn]-'0'; if(ck>=0) tmpsum+=k[ck]-'0'; tmpsum+=carry; carry = tmpsum/10; out = (char)(tmpsum%10+'0')+out; } if(carry>0) out = (char)(carry+'0') + out; cout<<out; return 0; } |