string add(string a,string b)
{
string s;
int len1,len2;
len1=a.size()-1;
len2=b.size()-1;
int flag=0;
while(len1>-1&&len2>-1)
{
int sum=flag+(a[len1--]-'0')+(b[len2--]-'0');
s+=char ((sum)%10+'0');
flag=sum/10;
}
while(len1>-1)
{
int sum=flag+(a[len1--]-'0');
s+=char ((sum)%10+'0');
flag=sum/10;
}
while(len2>-1)
{
int sum=flag+(b[len2--]-'0');
s+=char ((sum)%10+'0');
flag=sum/10;
}
if(flag)
s+=char('0'+flag);
int len=s.size()-1;
while(len>-1)
{
if(s[len]!='0')
break;
s=s.substr(0,s.size()-1);
len--;
}
for(int i=0;i<s.size()/2;i++)
{
char c=s[i];
s[i]=s[s.size()-i-1];
s[s.size()-i-1]=c;
}
if(s.size()==0)
s+='0';
return s;
}
高精度加法
最新推荐文章于 2022-11-25 19:56:12 发布