1 string sum(string s1,string s2) 2 { 3 if(s1.length()<s2.length()) 4 { 5 string temp=s1; 6 s1=s2; 7 s2=temp; 8 } 9 int i,j; 10 for(i=s1.length()-1,j=s2.length()-1;i>=0;i--,j--) 11 { 12 s1[i]=char(s1[i]+(j>=0?s2[j]-'0':0)); //注意细节 13 if(s1[i]-'0'>=10) 14 { 15 s1[i]=char((s1[i]-'0')%10+'0'); 16 if(i) s1[i-1]++; 17 else s1='1'+s1; 18 } 19 } 20 return s1; 21 }
参考资料