#include<iostream>
using namespace std;
void citire(int a[1000], int &n)
{
char cif;
int i;
cin>>n;
for(i=n;i>=1;i--)
{ cin>>cif;
a[i]=cif-48;//transform sin cifra in numar (cifa este caracter)
}
}
int maimare(int a[1000], int n, int b[1000], int m)
{
int i;
if(n>=m) return 1;
else if(n<m) return 0;
else
{
i=m;
while(a[i]==b[i]) i--;
if(a[i]>=b[i]) return 1;
else return 0;
}
}
int main()
{
int a[1000],b[1000],s[1000],n,m,p,i,k,semn,aux;
citire(a,n);
citire(b,m);
if(maimare(a,n,b,m)==0)
{//b>a
p=m;
for(i=n+1;i<=m;i++) a[i]=0;
for(i=1;i<=p;i++) s[i]=b[i]; //pun pe b in s
semn=-1;
for(i=1;i<=p;i++) b[i]=a[i];// pun pe a in b;
}
else //a>b
{ p=n;
for(i=m+1;i<=n;i++) b[i]=0;
for(i=1;i<=p;i++) s[i]=a[i]; //pun pe a in s;
}
for(i=1;i<=p;i++)
{
if(s[i]>=b[i]) s[i]=s[i]-b[i];
else
{ //ma imprumut
k=i+1;
while(s[k]==0) { s[k]=9; k++;}
s[k]--;
s[i]=10+s[i]-b[i];
}
}
if(semn==-1) cout<<"-";
while(s[p]==0) p--;
for(i=p;i>=1;i--) cout<<s[i];
return 0;
}
|