zoukankan      html  css  js  c++  java
  • △UVA424

     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

    自己写的太麻烦了,学习了别人的代码。

     1 #include<cstdio>
     2 #include<string.h>
     3 
     4 #define maxn 1005
     5 
     6 int sum[maxn];
     7 
     8 void add(char *str)
     9 {
    10     int i,j,len;
    11     len=strlen(str);
    12     for(i=maxn-1,j=len-1;j>=0;i--,j--) //sum[maxn-1]存的是个位,sum[maxn-2]存的是十位……
    13     {
    14         sum[i]+=str[j]-'0';//字符串,别忘了-'0'
    15     }
    16     for(i=maxn-1;i>0;i--)
    17     {
    18         if(sum[i]>9) //需要进位
    19         {
    20             sum[i]-=10;
    21             sum[i-1]++;
    22         }
    23     }
    24 }
    25 
    26 int main()
    27 {
    28     char s[maxn];
    29     int i;
    30     memset(sum,0,sizeof(sum));
    31     while(gets(s) != 0)
    32     {
    33         if(strcmp(s,"0") == 0)
    34             break;
    35         add(s);
    36     }
    37     for(i=0;i<maxn;i++)
    38     {
    39         if(sum[i]) //忽略前导0
    40             break;
    41     }
    42     for(;i<maxn;i++)
    43     {
    44         printf("%d",sum[i]);
    45     }
    46     printf("
    ");
    47     return 0;
    48 }
     
  • 相关阅读:
    mysql之创建数据库,创建数据表
    mysql之group by,order by
    一个人选出2门以上不及格的课程sql语句
    GIt入门
    数据库索引工作原理
    题目:50个人围城一圈数到3和3的倍数时出圈,问剩下的人是谁?原来的位置是多少?
    约瑟夫环:递归算法
    K-means算法的java实现,聚类分析681个三国武将
    java用一个for循环输出99乘法表
    写一个基于UDP协议的聊天小程序
  • 原文地址:https://www.cnblogs.com/youdiankun/p/3687241.html
Copyright © 2011-2022 走看看