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
|
#include<iostream> using namespace std; int tmp; int highplus(char n1,char n2){ int n=(n1-'0')+(n2-'0')+tmp; tmp=n/10; if(n+'0'<='9'){ return n+'0'; }else return n-10+'0'; } string expand(string num1,string num2){ string num=""; for(int i=1;i<=num1.length()-num2.length();i++){ num+='0'; } num+=num2; return num; } int main(){ string num1,num2; cin>>num1>>num2; if(num1.length()>num2.length()){ num2=expand(num1,num2); }else num1=expand(num2,num1); string result=""; for(int i=num1.length()-1;i>=0;i--){ result+=highplus(num1[i],num2[i]); } if(tmp+'0'!='0'){ result+=tmp+'0'; } for(int i=result.length()-1;i>=0;i--){ cout<<result[i]; } cout<<endl; return 0; }
|