Integer Inquiry
Time
Limit: 2000/1000 MS (Java/Others) Memory Limit:
65536/32768 K (Java/Others)
Total Submission(s):
11678 Accepted Submission(s):
2936
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 <stdio.h>
3 #include <stdlib.h>
4 #include <algorithm>
5 #include <cstring>
6 #include <math.h>
7 #include <ctime>
8 using namespace std;
9 #define maxn 32768
10 void add(char a[],char b[],char back[])
11 {
12 int i,j,k,up,x,y,z,l;
13 char *c;
14 if (strlen(a)>strlen(b)) l=strlen(a)+2; else l=strlen(b)+2;
15 c=(char *) malloc(l*sizeof(char));
16 i=strlen(a)-1;
17 j=strlen(b)-1;
18 k=0;up=0;
19 while(i>=0||j>=0)
20 {
21 if(i<0) x=‘0‘; else x=a[i];
22 if(j<0) y=‘0‘; else y=b[j];
23 z=x-‘0‘+y-‘0‘;
24 if(up) z+=1;
25 if(z>9) {up=1;z%=10;} else up=0;
26 c[k++]=z+‘0‘;
27 i--;j--;
28 }
29 if(up) c[k++]=‘1‘;
30 i=0;
31 c[k]=‘\0‘;
32 for(k-=1;k>=0;k--)
33 back[i++]=c[k];
34 back[i]=‘\0‘;
35 }
36
37 int main()
38 {
39 int n;
40 scanf("%d",&n);
41 char a[maxn]="0";
42 while(n--)
43 {
44 char b[maxn],c[maxn];
45 memset(b,‘\0‘,sizeof(b));
46 memset(c,‘\0‘,sizeof(c));
47 memset(a,‘\0‘,sizeof(a));
48 a[0]=‘0‘;
49 while(1)
50 {
51 scanf("%s",b);
52 if(b[0]==‘0‘)
53 break;
54 add(a,b,c);
55 strcpy(a,c);
56 memset(b,‘\0‘,sizeof(b));
57 memset(c,‘\0‘,sizeof(c));
58 }
59 if(n==0)
60 printf("%s\n",a);
61 else
62 printf("%s\n\n",a);
63
64 }
65 return 0;
66 }
hdu acm-1047 Integer Inquiry(大数相加),布布扣,bubuko.com