POJ 1503(若干个大数相加)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Albert_Bolt/article/details/82533409

Integer Inquiry

Time Limit: 1000MS Memory Limit: 10000K

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

思路:

若干个大数相加

AC代码:

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
#define maxn 1010
#define INF 0x3f3f3f3f
int sum[102];
char s[101];

int main()
{
    int len;
    while(cin>>s)
    {
        if(strcmp(s,"0")==0) break;
        len=strlen(s);
        for(int i=0;i<len;i++)
        {
            sum[i]+=s[len-i-1]-'0';
        }
        for(int i=0;i<=101;i++)
        {
            if(sum[i]>=10)
            {
                sum[i+1]+=sum[i]/10;
                sum[i]%=10;
            }
        }
    }
    int end=101;
    while(sum[end]==0)
    {
        end--;
    }
    for(int i=end;i>=0;i--)
    {
        cout<<sum[i];
    }
    cout<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Albert_Bolt/article/details/82533409