UVa 424 Integer Inquiry 【大数相加】

解题思路:因为给定的数据是多组,所以我们只需要多次做加法就可以了,将上一次的和又作为下一次加法运算的一个加数。

反思:还是题意理解不够清楚,最开始以为只是算三个大数相加,后来才发现是多个,然后注意到当输入a的第一个字符为0的时候结束运算,输出结果。

 Integer Inquiry 

 

One of the firstusers of BIT's new supercomputer was Chip Diller. He extended his explorationof powers of 3 to go from 0 to 333 and he explored taking various sums of thosenumbers.

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

Input

The input willconsist of at most 100 lines of text, each of which contains a singleVeryLongInteger. Each VeryLongInteger will be 100 or fewer characters inlength, and will only contain digits (no VeryLongInteger will be negative).

The final inputline will contain a single zero on a line by itself.

Output

Your programshould output the sum of the VeryLongIntegers given in the input.

Sample Input

123456789012345678901234567890

123456789012345678901234567890

123456789012345678901234567890

0

Sample Output

370370367037037036703703703670

 

#include<stdio.h>

#include<string.h>

#define max 1000

void add(char a[],char b[],char c[])

{

    char m[max],n[max];

    int i;

    int len1 ,len2;

    int flag=0;

    memset(m,0,sizeof(m));

    memset(n,0,sizeof(n));

    len1=strlen(a);

    len2=strlen(b);

    for(i=0;i<len1;i++)

    {

        m[i]=a[len1-i-1]-'0';

    }

    for(i=0;i<len2;i++)

    {

        n[i]=b[len2-i-1]-'0';

    }

    for(i=0;i<=len1||i<=len2;i++)

    {

        c[i]=m[i]+n[i]+flag;

        flag=c[i]/10;

        c[i]=c[i]%10+'0';

    }

}

void shuchu(char c[])

{

    int i,j;

    int len;

    len=strlen(c);

    for(i=len-1;c[i]=='0';i--);

    for(j=i;j>=0;j--)

    {

        printf("%c",c[j]);

    }

    printf("\n");

}

int main()

{

    char a[max],b[max],c[max];

    int i,j,len,tag=1;

     memset(b,0,sizeof(b));

     for(j=1;j<200&&tag;j++)

     {

         scanf("%s",&a);

         if(a[0]=='0')

         tag=0;

         memset(c,0,sizeof(c));

        add(a,b,c);

        len=strlen(c);

        for(i=0;i<len;i++)

        {

            b[i]=c[len-i-1];

        }

     }

     shuchu(c);



}

  

 

你可能感兴趣的:(Integer)