HDU 1047 多个大数相加

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 1047 多个大数相加相关的知识,希望对你有一定的参考价值。

Integer Inquiry

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 16773    Accepted Submission(s): 4326


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
 
 
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 char sa[1000],sb[1000];
 6 int a[1000],b[1000],c[1000];
 7 void add(char sa[1000],char sb[1000]){
 8         int lena=strlen(sa);
 9         int lenb=strlen(sb);
10         int lenc=(lena>lenb?lena:lenb);
11         memset(a,0,sizeof(a));
12         memset(b,0,sizeof(b));
13 
14         for(int j=0;j<lena;j++){
15             a[lena-1-j]=sa[j]-0;
16         }
17         for(int j=0;j<lenb;j++){
18             b[lenb-1-j]=sb[j]-0;
19         }
20         memset(c,0,sizeof(c));
21         for (int ii=0; ii<lenc; ii++) {
22             c[ii]=a[ii]+b[ii]+c[ii];
23             if (c[ii]>=10) {
24                 c[ii+1]=1;
25                 c[ii]-=10;
26             }
27         }
28         if (c[lenc]>0) lenc++;
29 
30         for (int ii=lenc-1; ii>=0; ii--){
31             sb[ii]=c[lenc-1-ii]+0;
32         }
33         sb[lenc]=\0;
34 
35 }
36 int main() {
37     int n,l1,l2,k,maxn=0;
38     scanf("%d",&n);
39     while(n--) {
40         k=0;
41         strcpy(sb,"0");
42         while(scanf("%s",sa),strcmp(sa,"0")) {
43             k++;
44             add(sa,sb);
45         }
46         puts(sb);
47         //putchar(‘\n‘);
48 
49         if(n)
50             putchar(\n);
51     }
52     return 0;
53 }

 

以上是关于HDU 1047 多个大数相加的主要内容,如果未能解决你的问题,请参考以下文章

HDU 1047 Integer Inquiry 大数相加 string解法

杭电1047——大数相加

(大数 string) Integer Inquiry hdu1047

HDU 1047 [Integer Inquiry] 高精度 格式

hdu1047

HDU 1316 (斐波那契数列,大数相加,大数比较大小)