zoukankan      html  css  js  c++  java
  • Integer Inquiry

    .Integer Inquiry

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


    This problem contains multiple test cases!

    The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

    The output format consists of N output blocks. There is a blank line between output blocks.


    Sample Input

    1



    123456789012345678901234567890
    123456789012345678901234567890
    123456789012345678901234567890
    0


    Sample Output

    370370367037037036703703703670

    测试有多个测试用例,每个测试用例给出的数不超过100组,求这些组数的和。和求N!一样,这也是一个大数高精度求和问题。

    while(cin>>str,strcmp(str,"0"))
            {
                m=strlen(str);
                for(i=m-1,j=m-1;i>=0;i--)
                    sum[j-i]+=str[i]-'0';


    这段代码就是在第一位前不断补充“0”。

    for(i=0;i<110;i++)
            {
                x=sum[i]+c;//c代表进位数
                sum[i]=x%10;
                c=x/10;
            }

    这段是循环的关键,每次循环加上上一次的进位。

    完整代码如下

    #include<iostream>
    #include<string>
    using namespace std;
    int main()
    {
        char str[100];
        int x,c,n,i,j,m,sum[110];
        cin>>n;//测试数据的个数
        while(n--)
        {
            memset(sum,0,sizeof(sum));//清0
            c=0;
            while(cin>>str,strcmp(str,"0"))
            {
                m=strlen(str);
                for(i=m-1,j=m-1;i>=0;i--)
                    sum[j-i]+=str[i]-'0';
            }
            for(i=0;i<110;i++)
            {
                x=sum[i]+c;//c代表进位数
                sum[i]=x%10;
                c=x/10;
            }
            i=109;//i代表测试数据当前的数
            while(sum[i]==0)
                i--;
            if(i<0)
                cout<<"0";
            for(;i>=0;i--)
                cout<<sum[i];
            cout<<endl;
            if(n)
                cout<<endl;
        }
        return 0;
    }


     


     

  • 相关阅读:
    进程实际操作篇2
    进程的实际操作篇1
    进程的理论知识
    解决套接字粘包,udp套接字对象的使用和socketserver模块实现并发
    day24-网络知识扫盲,socket的基本使用
    day23-网络编程之互联网基础,tcp/ip协议详细介绍
    day21-多态和多态性,鸭子类型,反射,内置方法,异常处理
    JAVA WEB小测
    JAVA动手动脑
    JAVA课上动手动脑问题2
  • 原文地址:https://www.cnblogs.com/u013533289/p/4477343.html
Copyright © 2011-2022 走看看