求两个不超过200位的非负整数的和。
Input
有两行,每行是一个不超过200位的非负整数,可能有多余的前导0。
Output
一行,即相加后的结果。结果里不能有多余的前导0,即如果结果是342,那么就不能输出为0342。
Sample Input
22222222222222222222
33333333333333333333
Sample Output
55555555555555555555
ac代码(注意0+0)
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <math.h>
#include <string.h>
#include <set>
using namespace std;
typedef unsigned long long ULL;
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;
}
return max;
}
int main()
{
string s1,s2,sum;
cin>>s1>>s2;
sum=add(s1,s2);
int i;
int len=sum.length();
for(i=0;sum[i];i++)
if(sum[i]=='0')
continue;
else
break;
if(i>=len) printf("0");
string sum2(sum.substr(i,len-i));//用substr函数截取字符串。
cout<<sum2<<endl;
return 0;
}