424 - Integer Inquiry

来源:互联网 发布:python输入ctrl c 编辑:程序博客网 时间:2024/06/07 00:43
IntegerInquiry 

One of the first users of BIT's new supercomputer was ChipDiller. He extended his exploration of powers of 3 to go from 0 to333 and he explored taking various sums of those numbers.

``This supercomputer is great,'' remarked Chip. ``I only wishTimothy were here to see these results.'' (Chip moved to a newapartment, once one became available on the third floor of theLemon Sky apartments on Third Street.)

Input

The input will consist of at most 100 lines of text, each ofwhich contains a single VeryLongInteger. Each VeryLongInteger willbe 100 or fewer characters in length, and will only contain digits(no VeryLongInteger will be negative).

The final input line will contain a single zero on a line byitself.

Output

Your program should output the sum of the VeryLongIntegers givenin the input.

SampleInput

1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678900

SampleOutput

370370367037037036703703703670
 
代码:
#include<stdio.h>
#include<string.h>
#include<ctype.h>
int main()
{
 int i,j,len,max,line,flen[101],c,sum;
 char text[102][101],text1[102][101],ans[103];
 while(1)
 {
  line=max=0;
  while(scanf("%s",text[line])==1)
  {
   len=strlen(text[line]);
   if(len==1 && text[line][0]=='0')break;
   else
   {
    for(i=0,j=len-1;i<len;i++,j--)
     text1[line][j]=text[line][i];
    flen[line]=len;
    if(len>max)max=len;
    line++;
   }
   
  }
        for(i=0;i<line;i++)
  {
   for(j=flen[i];j<max-1;j++)
    text1[i][j]='0';
   text1[i][j]='\0';
  }
  c=0; 
  for(i=0;i<max;i++)
  {
   sum=0;
   for(j=0;j<line;j++)
    sum=sum+(text1[j][i]-'0');
   sum=sum+c;
   ans[i]=(sum)+'0';
   c=sum/10;
  }
  if(c!=0)
  {
   if(c>10)
   {
    ans[i++]=(c)+'0';
       ans[i]=(c/10)+'0';
   }
   else
    ans[i]=c+'0';
   for(j=i;j>=0;j--)
      printf("%c",ans[j]);
  }
  else
  {
   for(j=i-1;j>=0;j--)
      printf("%c",ans[j]);
  }
  printf("\n");
     break;
 }
    return 0;
}
 
原创粉丝点击