#include <cstdio>
#include <string.h>
#include <ctype.h>
#include <string>
#include<cctype>
#include<algorithm>
#include<map>
#include<iostream>
#include<sstream>
using namespace std;
string add(string s1,string s2)
{
string max,min;
if(s1.length()>s2.length())
{
max=s1;min=s2;
}
else
{
max=s2;min=s1;
}
int l1=max.length(),l2=min.length();
for(int i=l1-1,j=l2-1;j>=0;i--,j--)
max[i]+=min[j]-'0';
for(int i=l1-1;i>=1;i--)
if(max[i]>'9')
{
max[i]-=10;
max[i-1]++;
}
if(max[0]>'9')
{
max[0]-=10;
max='1'+max;
}
int i;
for(i=0;i<max.size();i++)
if(max[i]!='0')
break;
if(i==max.size())
return "0";
else
return max.erase(0,i);
}
int main()
{
ios::sync_with_stdio(false);cin.tie(0);
string a,b;
while(cin>>a>>b)
{
cout<<add(a,b)<<endl;
}
return 0;
}
000 + 000 = 0
高精度大数相加模板
最新推荐文章于 2019-12-02 22:05:34 发布