1996: Super Addition
| Result | TIME Limit | MEMORY Limit | Run Times | AC Times | JUDGE |
|---|---|---|---|---|---|
| 60s | 10240K | 2026 | 141 | Standard |
The sum of any two integers A=a1a2...an and B=b1b2...bn is very easy to make(of course ai or bi may be zero), especially to programmers, because they can let the power overwhelming computer help them to do the work. But what if n becomes huge? For example, n exceeds 2^32. For such A and B, a program is unable to store them, say nothing of making their sum. But if you are clever enough, who can say you won't do the work?
Input
The input contains several test cases. Each test case consists of N+1 lines. The first line of each test case contains a single integer N(N<=10000000). The next N lines each contains two integers(from 0 to 9) representing ai and bi. For example, if A=1234 and B=567, then the input may be like this:
4
1 0
2 5
3 6
4 7
The last test case marks by N=-1, which you should not proceed.
Output
For each test case, print the sum of A and B in a single line.
Notice: The output integer may be very large(containing more than N digits).
Sample Input
4
1 0
2 5
3 6
4 7
-1
Sample Output
1801
This problem is used for contest: 3
#include<stdio.h>
#include<string.h>
const int max=2200000;
int a[max+1];
int main()
{
int i,j,aa,b,n,ta,tb;
while(scanf("%d",&n)==1&&n!=-1)
{
memset(a,0,sizeof(a));
ta=tb=0;
for(i=0;i<n%9;i++)
{
scanf("%d%d",&aa,&b);
ta=ta*10+aa;
tb=tb*10+b;
}
a[max-n/9]=ta+tb;
int l=max-n/9;
for(i=0;i<n/9;i++)
{
ta=tb=0;
for(j=0;j<9;j++)
{
scanf("%d%d",&aa,&b);
ta=ta*10+aa;
tb=tb*10+b;
}
a[++l]=ta+tb;
}
for(i=max;i>=0;i--)
{
if(a[i]>=1000000000)
{
a[i-1]+=1;
a[i]-=1000000000;
}
}
int flag=0;
int mm=max;
for(i=0;i<=max;i++)
{
if(a[i]){ mm=i;break;}
}
printf("%d",a[mm]);
for(i=mm+1;i<=max;i++)
{
printf("%09d",a[i]);
}
printf("/n");
}
return 0;
}
本文介绍了一个编程挑战——处理极大整数的加法运算。输入包含多个测试案例,每个案例有多行数字,需实现算法计算这些巨大数值的和,并正确输出结果。文章提供了完整的C语言程序代码实现。
24万+

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



