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;
}