emm
原理和高精度加是一样的
就是需要判断两个数大小之后是否先输出一个负号
别忘了把大的数放在上面减下面
然后上代码
#include<cstdio> #include<iostream> #include<cstring> #include<cmath> #define N 100005 using namespace std; char a[N]= {0},b[N]= {0}; int c[N]= {0},d[N]= {0},h[N]= {0}; int n1,n2,i,flag=0,l=0; int main() { //freopen("test.in","r",stdin); //freopen("test.out","w",stdout); cin >> a >> b; n1=strlen(a); n2=strlen(b); for( i=0; i<n1/2; ++i)swap(a[i],a[n1-i-1]); for( i=0; i<n2/2; ++i)swap(b[i],b[n2-i-1]); for(i=0; i<n1; ++i) c[i]=a[i]-'0'; for(i=0; i<n2; ++i) d[i]=b[i]-'0'; if(n2>n1) { for(i=0; i<n2; ++i) swap(c[i],d[i]); flag=1; } if(n1>n2) swap(n1,n2); for( i=0; i<n2; ++i) h[i]=c[i]-d[i]; for( i=0; i<n2; ++i) { if(h[i]<0) { h[i]+=10; h[i+1]--; } } if(flag==1) cout << '-'; for(i=n2-1; i>=0; i--) { if(l==0) { if(h[i]!=0) { l=1; cout << h[i]; continue; } } if(l!=0) { cout << h[i]; } } return 0; }