Integer Inquiry
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 15311 Accepted Submission(s): 3928
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
题意:
给出多组数据,每组数据由多个位数非常大的数组成,以0 为每组数据的结束标志,求得该组数据的和(多个大数相加),
输出每两组数据之间有空行,最后一组数据后无空行
分析:
其实刚拿到个题,感觉很复杂,是不是还需要把大数a+b的程序改成一个子函数来多次调用呢??感觉真的好复杂,结果,看了朋友的思路,发现自己想的复杂了,完全不必要把每次的加法都严格按大数a+b来运算,只需要能加上就行,然后根据题意给出的条件判断输出................
提示:
这个题的数据特别变态,很多极端情况都需要考虑到,否则就wrong answer,给大家几组数据......
#include<stdio.h>
#include<string.h>
int main()
{
char x[1005];
int n,i,len,s[1005],max;
scanf("%d",&n);
getchar();
while(n--)
{
memset(x,0,sizeof(x));
memset(s,0,sizeof(s));
max=0;//用来标记最大位数
while(scanf("%s",x)&&strcmp(x,"0")!=0)
{
len=strlen(x);
max=(max>len)?max:len;//更新和判断最大位数
for(i=0;i<len;++i)
{
s[i]+=x[len-i-1]-'0';//每次输入都累加到表示结果的数组上
if(s[i]>=10)//考虑进位
{
s[i]-=10;
++s[i+1];
}
}
}
for(i=max;i>0;--i)//如果不是为了判断类似00000的特殊情况,这个循环大可不必,坑死人的数据......
{
if(s[i]!=0)
{
break;
}
}
while(i>=0)//上面的循环肯定会留下一个s[0]...嘿嘿
{
printf("%d",s[i]);
i--;
}
printf("\n");
if(n>0)//注意格式.....
{
printf("\n");
}
}
return 0;
}