用一个二维数组存下所有的要加的数,记得-‘0’,之后加起来就行了
#include<iostream>
#include<stdio.h>
#include<cstring>
using namespace std;
const int maxn=200+5;
char a[maxn][maxn];
char s[maxn];
char answer[maxn];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int k=0;
int maxlen=1;
while(scanf("%s",s)!=EOF&&strcmp(s,"0"))
{
int len=strlen(s);
if(len>maxlen)maxlen=len;
for(int i=0;i<len;i++)
{
a[k][i]=s[len-i-1]-'0';
}
k++;
}
memset(answer,0,sizeof(answer));
for(int i=0;i<maxlen+2;i++)
{
int sum=0;
for(int j=0;j<k;j++)
{
sum+=a[j][i];
}
int x=sum%10;
int y=sum/10;
answer[i]+=x;
answer[i+1]+=y;
}
int i;
for(i=maxlen+2;i>=0;i--)
if(answer[i]!=0)break;
for(int j=i;j>=0;j--)
{
printf("%d",answer[j]);
}
printf("\n");
}
return 0;
}