AcWing 792. 高精度减法
原题链接
简单
#include<bits/stdc++.h>
using namespace std;
vector<int> res,x,y;
bool vector_swap(vector<int> x,vector<int> y)
{
if(x.size()>y.size())return true;
if(x.size()<y.size())return false;
for(int i=0;i<x.size();i++)
{
if(x[i]>y[i])return true;
if(x[i]<y[i])return false;
}
return true;
}
vector<int> minuse(vector<int> x,vector<int> y)
{
int t=0;
reverse(x.begin(),x.end());
reverse(y.begin(),y.end());
for(int j=0;j<x.size();j++)
{
int s=0;
if(j<y.size())s-=y[j];
s+=(x[j]+t);
if(s>=0)res.push_back(s),t=0;
else res.push_back(s+10),t=-1;
}
int i=res.size()-1;
while(i>0&&res[i]==0)i--,res.pop_back();
return res;
}
int main()
{
string a,b;
cin>>a>>b;
for(int i=0;i<a.size();i++)x.push_back(a[i]-'0');
for(int i=0;i<b.size();i++)y.push_back(b[i]-'0');
if(vector_swap(x,y)==false)swap(x,y),cout<<'-';
minuse(x,y);
for(int i=res.size()-1;i>=0;i--)
cout<<res[i];
return 0;
}