码迷,mamicode.com
首页 > 其他好文 > 详细

UVa 424 Integer Inquiry 【大数相加】

时间:2014-11-24 07:35:30      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:blog   io   ar   os   sp   for   strong   数据   on   

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

反思:还是题意理解不够清楚,最开始以为只是算三个大数相加,后来才发现是多个,然后注意到当输入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);

}

  

 

UVa 424 Integer Inquiry 【大数相加】

标签:blog   io   ar   os   sp   for   strong   数据   on   

原文地址:http://www.cnblogs.com/wuyuewoniu/p/4117869.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!