Integer Inquiry
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 30012   Accepted: 11690

Description

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

123456789012345678901234567890
123456789012345678901234567890
123456789012345678901234567890
0

Sample Output

370370367037037036703703703670
大数相加的问题,远超过64位,必须用数组。需要注意的是输入数据有些有前导零,也就是当数组第一个元素是0但数组的总位数>1时不算输入结束
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
    char temp[105];  //每次输入temp
    int out[121];   //out保存最终结果
    memset(out,0,sizeof(out));
    int i,j;
    while(cin>>temp)
    {
        int len=strlen(temp),sum=0;
        if(len==1&&temp[0]=='0') //当输入结束时就跳出
            break;
        for(i=120,len=len-1;len>=0;i--,len--)//每输入一个大树就执行一次加法
        {
             sum=(temp[len]-'0')+out[i];
             out[i]=sum%10;
             out[i-1]=out[i-1]+sum/10;
        }
        int k=i;
        for(;k>0;k--)  //对out进行疏导,保证其每一位的值都小于10
        {
            out[k-1]=out[k-1]+out[k]/10;
            out[k]=out[k]%10;
        }
    }
    for(i=0;out[i]==0;i++)  最终输出结果必须从有效数字非0开始
        j=i;
    for(j=j+1;j<=120;j++)
        cout<<out[j];
    cout<<endl;
    return 0;
}
posted on 2014-09-20 07:50  星斗万千  阅读(120)  评论(0编辑  收藏  举报