Integer Inquiry
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8767 Accepted Submission(s): 2227
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.)
``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.
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.
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
Source
本题的难点有两个:一是,大数的连加;二是,特殊数据。
大数的连加我采用的是大数各个位上的数字相加,然后该进位的进位,该取余的取余;特殊数据一开始真的没有考虑到,问题描述中只是说每一个VeryLongInteger不是负整数,但没说不可以是0,所以输入数据可以是“000\n00\n0”之类的!
View Code
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 int main(int argc, char *argv[]) 6 { 7 int n, num[105][105], cnt, i, j, len; 8 char s[105][105]; 9 scanf( "%d", &n ); 10 while( n-- ) 11 { 12 memset(s,0,105*105*sizeof(char)); 13 memset(num,0,105*105*sizeof(int)); 14 cnt = 0; 15 while( (scanf( "%s", s[cnt++])!= EOF)&&( strcmp(s[cnt-1],"0") != 0 ) ) 16 ; 17 for( i = 0; i < cnt; i++ ) 18 { 19 len = strlen(s[i]); 20 for( j = len-1; j >= 0; j-- ) 21 num[i][len-j-1] = s[i][j] - '0'; 22 } 23 for( i = 0; i < 105; i++ ) 24 for( j = 1; j < cnt; j++) 25 { 26 num[0][i] += num[j][i]; 27 if( num[0][i] >= 10 ) 28 { 29 num[0][i+1] += (num[0][i]/10); 30 num[0][i] = num[0][i] % 10; 31 } 32 } 33 for( i = 104; i >= 0; i-- ) 34 if( num[0][i] != 0 ) 35 break; 36 if( i != -1 ) 37 { 38 for( ; i >= 0; i-- ) 39 printf( "%d",num[0][i]); 40 printf( "\n" ); 41 } 42 else 43 printf( "0\n" ); 44 if( n != 0 ) 45 printf( "\n" ); 46 } 47 48 49 // system("PAUSE"); 50 return 0; 51 }