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
|
string (string num1, string num2) { if(num1.size()==0)return num2; if(num2.size()==0)return num1; string res; int long_index=0; int short_index=0; if(num1.size()>=num2.size()) { long_index=num1.size(); short_index=num2.size(); } else { long_index=num2.size(); short_index=num1.size(); swap(num1,num2); } int carry=0; while(short_index>0) { int temp=num1[--long_index]-'0'+num2[--short_index]-'0'+carry; res+='0'+(temp)%10; carry=temp/10; } while(long_index>0) { int temp=num1[--long_index]-'0'+carry; res+=temp%10+'0'; carry=temp/10; } if(carry==1)res+=1+'0'; reverse(res.begin(),res.end()); return res; }
|
近期评论