424 - Integer Inquiry

来源:互联网 发布:淘宝提交需求后然后呢 编辑:程序博客网 时间:2024/05/22 01:56

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

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

Input

The input will consist of at most 100 lines of text, each of which contains a single VeryLongInteger. Each VeryLongInteger will be 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 by itself.

Output

Your program should output the sum of the VeryLongIntegers given in the input.

Sample Input

1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678900

Sample Output

370370367037037036703703703670
--------------------------------------------------------------------------------------------
1.输入的字符串长度可能不一样,要求出最长的字符串长度
2.最高位进位也要考虑,当最高位的后一位是1时,长度加1
#include <stdio.h>#include <string.h>char num[110];char sum[110];int  max  = 0; void add(char *s1,char *s2)  {      int i,j,a[110] = {0},b[110] = {0};      int l1 = strlen(s1);      int l2 = strlen(s2);    if (l1 > l2)      max = l1;    else      max = l2;      for(i = l1-1,j = 0;i >= 0;i--,j++)          a[j] = s1[i]-'0';      for(i = l2-1,j = 0;i >= 0;i--,j++)          b[j] = s2[i]-'0';      for(i = 0;i < max;i++)      {      a[i] += b[i];      if(a[i] >= 10) a[i+1] += a[i]/10,a[i] %= 10;      }     if(a[i]) max++; //最高位是1的时候,长度加1     for(i = 0,j = max-1;j >= 0;j--,i++)    {      sum[i] = a[j]+'0';                       }     sum[i] = '\0';//strlen不包括\0 } int main(){   while(1)    {          gets(num);        if (num[0] == '0') break;        add(num,sum);    }   printf("%s\n",sum);   return 0;    } 


0 0