A - B problem(大数减法)(18.12.2)

A - B problem

Description

Now, Give you two intgers A and B , Please calculate the value of A minus B.Attation: A、B and A−B are all non-negative numbers.

Input

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

Output

For each case, Please output the value of A minus B

Sample Input 1

5 3
4 1

Sample Output 1

2
3

今天的这一题是减法问题,只需要模拟一下我们平时自己手算的过程就可以了,从个位开始减,减不开的话上更高位去借。因为题目说不会出现答案是负数的情况,所以就简单了。

下面是代码:

#include<stdio.h>
#include<string.h>
int main()
{
    char a[1000],b[1000];
    int s[1000],len1,len2,i,j;
    while(scanf("%s %s",a,b)!=EOF)
    {
        for(i=0;i<1000;i++)  //先初始化s
            s[i]=0;
        len1=strlen(a);len2=strlen(b);
        for(i=len1-1,j=0;i>=0;i--,j++)  //先用s储存一下a的每一位 由个位开始
        {
            s[j]+=(a[i]-'0');
        }
        for(i=len2-1,j=0;i>=0;i--,j++)  //开始减
        {
            if(s[j]>=b[i]-'0')   //r如果该位足够 直接减就可以
            {
                s[j]-=(b[i]-'0');
            }
            else   //如果不够 向更高位借位
            {
                s[j]=s[j]+10-(b[i]-'0');
                s[j+1]-=1;
            }
        }
        for(i=len1-1;i>=0;i--)  //又最后开始判断 找到第一个不是0的值 停止 并记录下i
            if(s[i]!=0)
                break;
        for(j=i;j>=0;j--)  //从记录好的那一位开始 输出
            printf("%d",s[j]);
        printf("\n");
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值