经典A+B

链接:https://ac.nowcoder.com/acm/contest/19859/F
来源:牛客网

题目描述

There are many difficulties in today’s contest, but this is definitely not one of them.
Because I want you to do it, the classic A+B.

输入描述:
First line an Integer N means n test cases.
Next 2N lines representing 2N groups of different Integers.
N<50.
THE LENGTH of every Integer will less than 5000.
All Integers will be greater than 0.
Some cases may have leading zero.
输出描述:
One line for each test cases reprecenting the answer of A+B.
Do not putout leading zero
示例1
输入
1
100000000000000
100000000000000
输出
200000000000000

#include<stdio.h>
#include<string.h>
int main()
{
    int n;
    scanf("%d",&n);
    while(n--)
    {
        char s1[5100],s2[5100];//A和B很大,所以用字符串形式输入
        scanf("%s",s1);
        scanf("%s",s2);
        int i,j,k;
        int a1[5100]={0},a2[5100]={0};
        int len1=strlen(s1);
        int len2=strlen(s2);
        for(i=len1-1,j=0;i>=0;i--,j++)
            a1[j]=s1[i]-'0';//再把每一位倒序存入数组中,方便相加
        for(i=len2-1,j=0;i>=0;i--,j++)
            a2[j]=s2[i]-'0';
        int a3[5100]={0};
        for(i=0;i<=len1+len2;i++)
        {
            a3[i]=a1[i]+a2[i];//两个数组每一位相加存入新数组中
        }
        for(i=0;i<=len1+len2;i++)
        {//处理逢10进一的情况
            if(a3[i]>=10)
            {
                a3[i]%=10;
                a3[i+1]++;
            }
        }
        int flag=0;
        for(i=len1+len2;i>=0;i--)
        {
            if(a3[i]!=0)
            {//确定数组的首位
                flag=1;
            }
            if(flag)//首位后的数都要输出
                printf("%d",a3[i]);
        }
        printf("\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值