CODE[VS] 3115 高精度练习之减法
题目链接.
日常水一水,没啥好说的,就是模拟
#include<bits/stdc++.h>
using namespace std;
int main()
{
char zf='?';
string a,b;
vector <int> c,d;
int cc[505]={0},dd[505]={0},ans[505]={0};
cin>>a>>b;
if(a.length()>b.length()) zf='+';
else if(a.length()<b.length())zf='-';
else {
for(int i=0;i<a.length();i++)
{
if(a[i]>b[i]){
zf='+';
break;
}else if(a[i]<b[i]){
zf='-';
break;
}
}
}
if(zf=='?'){
printf("0");
return 0;
}
else
if(zf=='-')swap(a,b);
// cout<<a<<endl<<b;
reverse(a.begin(),a.end());
reverse(b.begin(),b.end());
for(int i=0;i<a.length();i++)cc[i]=a[i]-'0';
for(int i=0;i<b.length();i++)dd[i]=b[i]-'0';
for(int i=0;i<max(a.length(),b.length());i++)ans[i]=cc[i]-dd[i];
for(int i=0;i<max(a.length(),b.length());i++){
if(ans[i]<0){
ans[i]=ans[i]+10;
ans[i+1]-=1;
}
}
if(zf=='-')printf("-");
int fei=0;
for(int i=max(a.length(),b.length())-1;i>=0;i--)
{
if(ans[i]==0&&fei==0)continue;
else fei=1;
printf("%d",ans[i]);
}
//if(ans[max(a.length(),b.length())-1]!=0)printf("%d",ans[max(a.length(),b.length())-1]);
return 0;
/*
reverse(a.begin(),a.end());
reverse(b.begin(),b.end());
int la=a.length(),lb=b.length();
int len=0,flag=0;
cout<<a<<' '<<b<<endl;
for(int i=0;i<max(la,lb);i++)
{
if(flag)
c.insert(c.begin(),a[i]+b[i]-'0'-'0'+1);
else
c.insert(c.begin(),a[i]+b[i]-'0'-'0');
flag=0;
if(c[i]>=10)
{
c[i]-=10;
flag=1;
}
}
if(flag)
c.insert(c.begin(),1);
for(int i=c.size()-1;i>=0;i--)
cout<<c[i]<<' ';
// cout<<a<<endl<<b;
*/
}