A-B problem

A-B problem

题目传送门
Description
Now, Give you two intgers A and B , Please calculate the value of A minus B.
Attation: A、Band A-B are all non-negative numbers.

Input
Each line will contain two integers AA and BB.Process to end of file.(EOFEOF)

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

Sample Input 1
5 3
4 1

Sample Output 1
2
3
由于这个题的数据比较大,无法用long long int 表示,所以要用数组模拟这一减法问题。要注意进行完一次运算后对数组进行初始化。

#include<stdio.h>
int main()
{
    char x[1000],y[1000];
    int l[1000],m[1000],len1,len2;
    while(scanf("%s%s",&x,&y)!=EOF)
    {
        for(int i=0;i<1000;i++)
            {l[i]=0;m[i]=0;}
        len1=strlen(x);len2=strlen(y);
        for(int i=0,j=len1-1;j>=0;j--,i++)
            l[i]=x[j]-'0';
        for(int i=0,j=len2-1;j>=0;j--,i++)
            m[i]=y[j]-'0';
        for(int i=0;i<len1;i++)
            l[i]=l[i]-m[i];
        for(int i=0;i<len1;i++)
        {
            if(l[i]<0){l[i]+=10;l[i+1]-=1;}
        }
        for(int i=len1-1;i>0;i--)
            printf("%d",l[i]);
        printf("%d\n",l[0]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43105110/article/details/84707781
A-B