A + B problem(大数加法)(18.11.30)

A + B problem

Description

Calculate A + B.

Input

Each line will contain two integers A and B. Process to end of file. (EOF)

Output

For each case, output A + B in one line.

Sample Input 1

2 3
4 5

Sample Output 1

5
9

题目很简单,但是因为所给数据没给范围,不能用long long储存,所以这一题我们需要模拟一下大数的加法,因为每个整数前都没有符号,所以这一题写起来也比较简单。

贴代码:

#include<stdio.h>
#include<string.h>
int main()
{
    char a[1000],b[1000];
    int i,s[1000],len1,len2,len,j;
    while(scanf("%s%s",a,b)!=EOF)  //用字符数组来储存数
    {
        for(i=0;i<1000;i++)
            s[i]=0;
        len1=strlen(a);len2=strlen(b);
        for(i=len1-1,j=0;i>=0;i--,j++)  //从个位开始,将每一位都分别加到整型数组s中,下同
            s[j]+=a[i]-'0';
        for(i=len2-1,j=0;i>=0;i--,j++)
            s[j]+=b[i]-'0';
        if(len1>len2)   //寻找s的最大长度
            len=len1;
        else
            len=len2;
        for(i=0;i<=len;i++)   //进位
            if(s[i]>9)
        {
            s[i]-=10;
            s[i+1]+=1;
        }
        if(s[len]!=0) //其实就是在判断最大一位是不是进位了
            len+=1;
        for(i=len-1;i>=0;i--)   //逆序输出
            printf("%d",s[i]);
        printf("\n");
    }
    return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值