64,646
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <string.h>
#include <malloc.h>
void add(char *a,char *b,char *c)
{
int len1 = strlen(a);
int len2 = strlen(b);
char *sum = (char *)malloc((len2>len1?len2:len1)+2);
memset(sum,0,(len2>len1?len2:len1)+2);
int i = len1-1,j = len2-1;
int count = (len2>len1?len2:len1);
while (i>=0 && j>=0)
{
sum[count] = sum[count] + a[i] + b[j] - '0';
if (sum[count] > '9')
{
sum[count-1] = (sum[count] - '0') / 10;
sum[count]=(sum[count] - '0')%10 + '0';
}
i--;
j--;
count--;
}
while (i>=0)
{
sum[count] = sum[count] + a[i];
if (sum[count] > '9')
{
sum[count-1] += (sum[count] - '0') / 10;
sum[count]=(sum[count] - '0')%10 + '0';
}
i--;
count--;
}
while (j>=0)
{
sum[count] = sum[count] + b[j];
if (sum[count] > '9')
{
sum[count-1] += (sum[count] - '0') / 10;
sum[count]=(sum[count] - '0')%10 + '0';
}
j--;
count--;
}
i = 0;
if (sum[count])
{
c[i] = sum[count] + '0';
i++;
}
count++;
while (sum[count])
{
c[i++] = sum[count++];
}
c[i] = '\0';
free(sum);
}
int main()
{
int a[100],b[100],c[101];
int N;
while(scanf("%d",&N) == 1)
{
char ch,ch1;
int block_start = 0;
int block_end = 0;
int i = 0;
for(i = 0;i!=N;i++)
{
int j = 0;
memset(c,0,sizeof(c));
memset(a,0,sizeof(a));
c[0] = '0';
while(1)
{
ch = getchar();
if (ch >='0' && ch <='9')
{
break;
}
}
a[j] = ch;
j++;
while(1)
{
while ((ch = getchar()) != '\n')
{
a[j++] = ch;
}
a[j] = '\0';
printf("%s\n",a);//惊奇的a
j = 0;
if (strcmp((char *)a,"0")==0)
{
N=0;
}
add((char *)c,(char *)a,(char *)c);
ch = getchar();
if (ch == '\n')
{
break;
}
}
printf("%s\n",c);
}
if(N==0)
{
break;
}
}
return 0;
}