HDU1047 Integer Inquiry

男娘i 2022-06-09 13:13 297阅读 0赞

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

Code:

  1. #include <iostream>
  2. #include <algorithm>
  3. #include <stdio.h>
  4. #include <cstdlib>
  5. #include <cstring>
  6. #include <cmath>
  7. #include <ctime>
  8. #include <ctype.h>
  9. using namespace std;
  10. char num[105];
  11. int sum[1000];
  12. void add(char a[])
  13. {
  14. int z=0;
  15. for(int i=strlen(a)-1;i>=0;i--)
  16. {
  17. sum[z]+=a[i]-'0';
  18. sum[z+1]+=sum[z]/10;
  19. sum[z]%=10;
  20. z++;
  21. }
  22. }
  23. int main()
  24. {
  25. int t,i;
  26. scanf("%d",&t);
  27. while(t--)
  28. {
  29. memset(num,' ',sizeof(num));
  30. memset(sum,0,sizeof(sum));
  31. while(scanf("%s",num)&&strcmp(num,"0"))
  32. add(num);
  33. for(i=999;i>=0;i--)
  34. if(sum[i])
  35. break;
  36. if(i<0)
  37. printf("0");
  38. for(;i>=0;i--)
  39. printf("%d",sum[i]);
  40. printf("\n");
  41. if(t!=0)
  42. printf("\n");
  43. }
  44. return 0;
  45. }

发表评论

表情:
评论列表 (有 0 条评论,297人围观)

还没有评论,来说两句吧...

相关阅读

    相关 1047. 编程团体赛(20)

    编程团体赛的规则为:每个参赛队由若干队员组成;所有队员独立比赛;参赛队的成绩为所有队员的成绩和;成绩最高的队获胜。 现给定所有队员的比赛成绩,请你编写程序找出冠军队。 输入