1151: 大整数加法

1151: 大整数加法

时间限制: 1 Sec  内存限制: 128 MB
提交: 1795  解决: 759
[提交][状态][讨论版][命题人:admin]

题目描述

比利经常会碰到超大整数的加法运算,而普通的计算器上无法进行。因此他想你帮他写一个程序来计算结果。

输入

输入数据有多组。首先输入一个整数T,表示有T组输入。

每组输入两个大整数,并用空格隔开。每个整数最多1000位。没有负数输入。

输出

对于每组输入,输出两个整数的和,单独占一行。

样例输入

2
1 2
112233445566778899 998877665544332211

样例输出

3
1111111111111111110

提示

来源

#include<stdio.h>
#include<string.h>
char m[1022],n[1022];
int c[1022];
int lres;
int lm,ln;
void update()
{
    if(c[lres] >=10)
        c[++lres]++,c[lres-1] %=10;
    else
        lres++;
}
int main()
{
    int t;
    scanf("%d",&t);
    getchar();
    while(t--)
    {
        memset(c,0,sizeof(c));
        scanf("%s%s",m,n);
        lm = strlen(m)-1;
        ln = strlen(n)-1;
        lres = 0;
        while(lm>=0&&ln>=0)
            c[lres] += m[lm--]-'0' + n[ln--] - '0',update();
        while(lm>=0)
            c[lres]+=m[lm--]-'0',update();
        while(ln>=0)
            c[lres] += n[ln--]-'0',update();
        int f = 0;
        for(int i=lres; i>=0; i--)
        {
            if(c[i]!=0) f=1;
            if(f == 1)
                printf("%d",c[i]);
        }
        printf("\n");
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值