游客 Signup | Login
中文 | En

3032 - 小俞同学的算术

小俞同学对天文很感兴趣,但是大家知道,用来计算天文的数字都是非常大的, 有的时候需要用到减法,现在她遇到难题了,需要知道a - b = ? ,所以又找到了你(怎么总能找到, 应该黑名单了)

Input

第一行一个数a(0<=a<=10500)

第二行一个数b(0<=b<=1e500)

并且保证(a >= b)。


Output

一行一个整数,并且需要必要的处理,结果应该是朴素的数字。

Examples

Input

2
1
4
4

Output

1
0

Solution C

#include<stdio.h>
#include<string.h>	
int X[510],Y[510];
int go(int b,int d)
{
	int c;
	X[b]=10+d;
	c=X[b+1]-1;
	if(c<0)
	go(b+1,-1);
	else X[b+1]=c;
}
int main()
{
	char A[510],B[510];

	int a,b,c,x,y,z,i,j,k,l,m,n,o,p,q,r,s,t;
	while(scanf("%s%s",&A,&B)!=EOF)
	{
		memset(X,0,sizeof(X));
		memset(Y,0,sizeof(Y));
		for(a=0,b=strlen(A)-1;b>=0;a++,b--)
		X[a]=A[b]-'0';
		for(a=0,b=strlen(B)-1;b>=0;a++,b--)
		Y[a]=B[b]-'0';
		for(b=501;b>=0;b--)
		{
			c=X[b]-Y[b];
			if(c>=0)
			X[b]=c;
			else 
			go(b,c);
		}
		for(b=501;b>=0&&X[b]==0;b--);
		if(b<0)printf("0\n");
		else 
		{
			for(b;b>=0;b--)
			printf("%d",X[b]);
			printf("\n");
		}
	}
	return 0;
}

Solution C++

#include<iostream>
#include<cstdlib>
#include<algorithm>
#include<cstdio>
#include<cstring>

using namespace std;

const int N = 505;

char a[N], b[N], t[N];

void sub(char s1[],char s2[],char t[])
{
    int i,l2,l1,k;
    l2=strlen(s2);l1=strlen(s1);
    t[l1]='\0';l1--;
    for (i=l2-1;i>=0;i--,l1--)
        {
        if (s1[l1]-s2[i]>=0)
            t[l1]=s1[l1]-s2[i]+'0';
        else
            {
            t[l1]=10+s1[l1]-s2[i]+'0';
            s1[l1-1]=s1[l1-1]-1;
            }
        }
    k=l1;
    while(s1[k]<0) {s1[k]+=10;s1[k-1]-=1;k--;}
    while(l1>=0) {t[l1]=s1[l1];l1--;}
loop:
    if (t[0]=='0')
        {
        l1=strlen(s1);
        for (i=0;i<l1-1;i++) t[i]=t[i+1];
            t[l1-1]='\0';
        goto loop;
        }
    if (strlen(t)==0) {t[0]='0';t[1]='\0';}
}

int main()
{
//    freopen("in.txt", "r", stdin);
//    freopen("out.txt", "w", stdout);
    while(~scanf("%s%s", a, b))
    {
        sub(a, b, t);
        printf("%s\n", t);
    }
    return 0;
}

Time Limit 1 second
Memory Limit 128 MB
Discuss Stats
上一题 下一题