UVA - 424 Integer Inquiry

来源:互联网 发布:免费的交友软件 编辑:程序博客网 时间:2024/06/05 08:19
 Integer Inquiry
Time Limit:3000MS    Memory Limit:0KB    64bit IO Format:%lld & %llu


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.
Ouput

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

123456789012345678901234567890
123456789012345678901234567890
123456789012345678901234567890
0

Sample Output

370370367037037036703703703670

PS:以前做过,不过方法不同。这次用递归特别方便,还有数组开大一点,还要防止越界。

#include <iostream>#include "algorithm"#include "cstdio"#include "cstring"using namespace std;string temp_in;int sum[50050], sum_temp[50050];void add(int cur, int data){    sum[cur] += data;    if(sum[cur] >= 10)    {        sum[cur] -= 10;        add(cur+1, 1);    }}int main(){   //freopen("1.in", "r", stdin);    int i, j, k;    while(cin>>temp_in)    {        if(temp_in.size() == 1 && temp_in[0] == 0) break;        string temp(temp_in.rbegin(),temp_in.rend());//逆序储存,方便进位        for(i = 0; i < temp.size(); i++)            sum_temp[i] = temp[i]-'0';        int cur = temp.size();//cout<<cur<<endl;        for(i = 0; i < cur; i++)        {            add(i, sum_temp[i]);        }    }    for(i = 50000; i >= 0; i--)        if(sum[i] != 0) break;    int len = i+1;//cout<<len<<endl;    for(i = len-1; i >= 0; i--)        cout<<sum[i];    cout<<endl;    return 0;}


0 0
原创粉丝点击