高精度减法
#include<iostream> using namespace std; string a,b,str; int main() { cin>>a; cin>>b; int sign = 0; if(a==b) { cout<<"0"<<endl; return 0; } if((a < b && a.size()==b.size()) || (a.size()<b.size())) { sign = 1; swap(a,b); } int len1 = a.length(); int len2 = b.length(); int tep = len1 - len2; int cf = 0; for(int i=len2-1;i>=0;i--) { if(a[i+tep]<b[i]+cf) { str = char(a[i+tep]-b[i]-cf+'0'+10) + str; cf = 1; }else{ str = char(a[i+tep]-b[i]-cf+'0')+str; cf = 0; } } for(int i =tep-1;i>=0;i--) { if(a[i]-cf>='0') { str = char(a[i]-cf)+str; cf = 0; }else{ str = char(a[i]-cf+10)+str; cf = 1; } } str.erase(0,str.find_first_not_of('0')); if(sign == 1) cout<<"-"; cout<<str<<endl; }
此高精度只能计算输入的两个整数都为正数。