高精度减法
作者:
愛_
,
2024-11-21 11:47:34
,
所有人可见
,
阅读 1
#include<bits/stdc++.h>
using namespace std;
vector<int> sub(vector<int>A,vector<int>B)
{
vector<int> c;
for(int i=0;i<A.size();i++)
{
int b =0;
if(i<B.size()) b =B[i];
if(A[i]>=b) c.push_back(A[i]-b);
else {
c.push_back(A[i]+10-b);
if(i+1<A.size()) A[i+1]--;
}
}
while(c.size()>1 and c.back()==0)c.pop_back();
return c;
}
int main()
{
string a,b;
cin>>a>>b;
vector<int>A,B;
for(int i =a.size()-1;i>=0;i--)A.push_back(a[i]-'0');
for(int i =b.size()-1;i>=0;i--)B.push_back(b[i]-'0');
if(a.size()<b.size() or a.size()==b.size() and A<B){
auto c= sub(B,A);
cout<<"-";
for(int i =c.size()-1;i>=0;i--)cout<<c[i];
}else{
auto c= sub(A,B);
for(int i =c.size()-1;i>=0;i--)cout<<c[i];
}
return 0;
}