题目地址:点击打开链接
C++代码:
#include <cstdio>
#include <cstring>
void add(char s1[],char s2[])
{
int len1=strlen(s1);
int len2=strlen(s2);
int i;
int c=0;
int len=len1>len2?len2:len1;
for(i=0;i<len;++i)
{
if(s1[i]-'0'+s2[i]-'0'+c>=10)
{
int t=s1[i]-'0'+s2[i]-'0'+c;
c=t/10;
*(s1+i)=t%10+'0';
}
else
{
*(s1+i)=s1[i]-'0'+s2[i]-'0'+c+'0';
c=0;
}
}
int len_max=len1>len2?len1:len2;
for(i=len;i<len_max;++i)
{
if(s1[i]+s2[i]-'0'+c>=10)
{
int t=s1[i]+s2[i]-'0'+c;
c=t/10;
*(s1+i)=t%10+'0';
}
else
{
*(s1+i)=s1[i]+s2[i]-'0'+c+'0';
c=0;
}
}
if(c>0)
*(s1+i)=c+'0';
}
char s[2000][2000];
char ans[2000];
int main()
{
int n;
while (true)
{
n=0;
memset(s,0,sizeof(s));
while (true)
{
if(scanf("%s",s[n])==EOF)
goto loop;
if(s[n][0]=='0')
break;
int i,j;
for(i=0,j=strlen(s[n])-1;i<j;++i,--j)
{
char c;
c=s[n][i];
s[n][i]=s[n][j];
s[n][j]=c;
}
++n;
}
memset(ans,0,sizeof(ans));
int i;
for(i=0;i<n;++i)
add(ans,s[i]);
for(i=199;i>=0&&ans[i]==0;--i);
if(i<0)
printf("0");
else
for(;i>=0;--i)
printf("%c",ans[i]);
printf("\n");
}
loop:
return 0;
}