Problem B: Integer Inquiry
Time Limit: 1 Sec Memory Limit: 64 MB
Submit: 1553 Solved: 368
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
HINT
大数加法
注意题目要求,控制输出格式弄好,别的都很好搞;就是注意下特例
1
0000
0000
0000
0
0
这个是是个坑。。。。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
char a[105];
int num[10005];
int main()
{
int n;
scanf("%d",&n);
getchar();
while(n--)
{
memset(num,0,sizeof(num));
while(gets(a)&&strcmp(a,"0"))
{
int len=strlen(a);
int j=10004;
for(int i=len-1;i>=0;i--)
{
int ans=a[i]-'0';
if(ans+num[j]>=10)
{
num[j]=num[j]+ans-10;
num[j-1]++;
}
else num[j]+=ans;
j--;
}
}
int flag=0;
for(int i=0;i<10005;i++)
{
if(num[i]!=0)
{
flag=1;
for(int j=i;j<10005;j++)
printf("%d",num[j]);
memset(num,0,sizeof(num));
printf("\n");
break;
}
}
if(flag==0) printf("0\n");
if(n!=0)
{
printf("\n");
}
}
return 0;
}
本文探讨了ProblemB:IntegerInquiry问题,这是一个关于大数加法的编程挑战,涉及读取多行输入,每行包含一个非常大的整数,并输出所有这些整数的总和。文章提供了一个C++实现的示例代码,展示了如何处理大数加法并正确格式化输出。

376

被折叠的 条评论
为什么被折叠?



