大数加法模板(可能有问题,目前没发现)
1 string add(string t1,string t2) 2 { 3 reverse(t1.begin(),t1.end()); 4 reverse(t2.begin(),t2.end()); 5 string ans; 6 ans.clear(); 7 int tmp=0,pos=0; 8 int s1,s2; 9 int len1=t1.size(),len2=t2.size(); 10 while(pos<len1||pos<len2) 11 { 12 if(pos<len1) 13 s1=t1[pos]-'0'; 14 else 15 s1=0; 16 if(pos<len2) 17 s2=t2[pos]-'0'; 18 else 19 s2=0; 20 ans+=((s1+s2+tmp)%10+'0'); 21 tmp=(s1+s2+tmp)/10; 22 pos++; 23 } 24 if(tmp)ans+=(tmp+'0'); 25 reverse(ans.begin(),ans.end()); 26 return ans; 27 }