视频链接:
#include <iostream> using namespace std; const int N = 110000; int A[N],B[N],C[N]; int la,lb,lc; bool cmp(int A[],int B[]){ if(la != lb) return la>lb; for(int i = la; ~i; i--) if(A[i]!=B[i]) return A[i]>B[i]; return true; //避免结果-0 } void sub(int A[],int B[]){ int t = 0; for(int i=0;i<=la;i++){ t = A[i]; t -= B[i]; if(t<0) A[i+1]--, t+=10; C[i] = t % 10; } while(la&&C[la]==0) la--;//处理前导0 } int main(){ string a, b; cin >> a >> b; la=a.size()-1,lb=b.size()-1; for(int i=la; ~i; i--) A[la-i]=a[i]-'0'; for(int i=lb; ~i; i--) B[lb-i]=b[i]-'0'; if(!cmp(A,B)) swap(A,B),swap(la,lb),cout<<'-'; sub(A, B); for(int i=la; ~i; i--) printf("%d",C[i]); return 0; }
#include <iostream> #include <vector> using namespace std; typedef vector<int> vi; vi A,B,C; bool cmp(vi &A, vi &B){ if(A.size() != B.size()) return A.size()>B.size(); for(int i = A.size()-1; ~i; i--) if(A[i]!=B[i]) return A[i]>B[i]; return true; //避免结果-0 } void sub(vi &A, vi &B){ int t = 0; for(int i = 0; i<A.size(); i ++){ t = A[i]; if(i<B.size()) t -= B[i]; if(t<0) A[i+1]--, t+=10; //借位 C.push_back(t % 10); } while(C.size()>1 && !C.back()) C.pop_back(); //处理前导0 } int main(){ string a, b; cin >> a >> b; for(int i=a.size()-1;~i;i--) A.push_back(a[i]-'0'); for(int i=b.size()-1;~i;i--) B.push_back(b[i]-'0'); if(!cmp(A, B)) swap(A,B), cout << '-'; sub(A, B); for(int i=C.size()-1; ~i; i--) cout << C[i]; }
标签:lb,la,int,vi,高精度,return,减法,502,size From: https://www.cnblogs.com/dx123/p/16654287.html