1996: Super Addition

1996: Super Addition


ResultTIME LimitMEMORY LimitRun TimesAC TimesJUDGE
60s10240K2026141Standard

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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值