zoukankan      html  css  js  c++  java
  • UVA424 Integer Inquiry

     Integer Inquiry 

    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


    很久很久没有做过高精度的题目了,作为最简单的高精度加法,要注意的是如下几点,第一,因为是数位达到上百位的大数,所以只能用字符串
    数组来存贮。第二,为了方便之后的相加操作,应该把字符串数组逆序转化为一个整型数组。第三,在控制进位的时候,应该用一个工具变量,
    假设为t,则t初始值为0.之后在循环体里t是等于对应位的两个数相加之后/10,使得下一次循环时,高位可以进位。第四,因为已经把原来读取
    的大数用字符数组读入后又逆序复制到一个新的整型数组里,所以此时下标小的保存的就是低位,这个时候再控制循环变量从低位一直加到高位,
    多余的位都是0,但是没关系,继续相加(如果不这样的话,最大的那一位可能会出现无法进位的情况,即读入的数是多少位,最后结果就是多少
    位,显然是错误的)。。。第五。在相加的时候,可以不必一位一位相加,可以4位4位相加,即万进制加法,其实万进制是很方便的一个方法,
    之后还会用万进制乘法,在节省时间方面是很有用的。
    下面是AC代码,还是采用一位一位相加,采用的基本上是上述思想



    #include <iostream>
    #include <cstring>
    using namespace std;
    char s[1000];
    int ss[1000];
    int sum[1000];
    int main()
    {
        memset (sum,0,sizeof(sum));
        int len;
        while (cin.getline(s,1000)&&s[0]!='0')
          {
              memset(ss,0,sizeof(ss));
              len=strlen(s);int t=0;
              for (int i=len-1,j=0;i>=0;i--,j++)
                {
                    ss[j]=s[i]-'0';
                }
              for (int k=0;k<1000;k++)
                {
                    int temp=sum[k]+ss[k]+t;
                        sum[k]=temp%10;
                        t=temp/10;
                }
          }
          int p;
          for (p=999;p>=0;p--)
           {
               if (sum[p]!=0) break;
           }
          for(int k=p;k>=0;k--)
           {
               cout<<sum[k];
           }
           cout<<endl;
        return 0;
    }
    
    
    

      



  • 相关阅读:
    数据结构01——线性表
    hdu 6069 Counting Divisors (唯一正整数分解定理+素数筛)
    hdu 6053 TrickGCD (莫比乌斯)
    hdu 1695 GCD(莫比乌斯入门)
    poj 2096 Collecting Bugs (概率dp)
    DC.p4: programming the forwarding plane of a data-center switch
    Packet Transactions: High-level Programming for Line-Rate Switches
    P4: Programming Protocol-Independent Packet Processors
    Improving Network Management with Software Defined Networking
    Are We Ready for SDN? Implementation Challenges for Software-Defined Networks
  • 原文地址:https://www.cnblogs.com/kkrisen/p/2866564.html
Copyright © 2011-2022 走看看