最近发现自己真的菜,连高精度都手写不出来了,在此插眼保存一份模板,以防忘记:
1.高精度加法:
#include<bits/stdc++.h> using namespace std; const int N=100005; string a,b; vector<int>add(vector<int>&A,vector<int> &B) { vector<int>C; int t=0; for(int i=0;i<A.size()||i<B.size();i++) { if(i<A.size()) t+=A[i]; if(i<B.size()) t+=B[i]; C.push_back(t%10); t/=10; } if(t) C.push_back(1); return C; } int main() { 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'); auto C=add(A,B); for(int i=C.size()-1;i>=0;i-- ) cout<<C[i]; cout<<endl; }
2.高精度减法:
#include<bits/stdc++.h> using namespace std; const int N=1e5+19; bool compare(vector<int>&A,vector<int>&B) { if(A.size()!=B.size()) return A.size()>B.size(); else { for(int i=A.size();i>=0;i--) { if(A[i]!=B[i]) { return A[i]>B[i]; } } } return true; } vector<int>sub(vector<int>&A,vector<int>&B) { int t=0; vector<int>C; for(int i=0;i<A.size();i++) { t=A[i]-t; if(i<B.size()) t-=B[i]; C.push_back((t+10)%10); if(t<0) t=1; else t=0; } while(C.size()>1&&C.back()==0) C.pop_back(); return C; } int main() { string a; string b; cin>>a>>b; vector<int>A,B,C; 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(compare(A,B)) { C=sub(A,B); for(int i=C.size()-1;i>=0;i--) cout<<C[i]; cout<<endl; }else { C=sub(B,A); printf("-"); for(int i=C.size()-1;i>=0;i--) cout<<C[i]; cout<<endl; } }
3.高精度乘法:
1.高精度X低精度
#include<bits/stdc++.h> using namespace std; const int N=100005; string a; int b; vector<int>mul(vector<int>&A,int &b) { vector<int>C; int t=0; for(int i=0;i<A.size()||t;i++) { if(i<A.size()) t+=A[i]*b; C.push_back(t%10); t/=10; } while(C.size()>1&&C.back()==0) C.pop_back(); return C; } int main() { cin>>a>>b; vector<int>A,B,C; 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'); C=mul(A,b); for(int i=C.size()-1;i>=0;i--) cout<<C[i]; cout<<endl; }
2.高精度X高精度
#include<bits/stdc++.h> using namespace std; const int N=100005; string a; string b; vector<int>mul(vector<int>&A,vector<int> &B) { vector<int>C(A.size()+B.size(),0); for(int i=0;i<A.size();i++) for(int j=0;j<B.size();j++) C[i+j]+=B[j]*A[i]; int t=0; for(int i=0;i<C.size()||t;i++) { t+=C[i]; C[i]=t%10; t/=10; } while(C.size()>1&&C.back()==0) C.pop_back(); return C; } int main() { cin>>a>>b; vector<int>A,B,C; 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'); C=mul(A,B); for(int i=C.size()-1;i>=0;i--) cout<<C[i]; cout<<endl; }
4.高精度除法
1.高精度除以低精度:
#include<bits/stdc++.h> using namespace std; const int N=100005; string a; int b; vector<int>div(vector<int>&A,int &b,int &r) { vector<int>C; for(int i=A.size()-1;i>=0;i--) { r=r*10+A[i]; C.push_back(r/b); r%=b; } reverse(C.begin(),C.end()); while(C.size()>1&&C.back()==0) C.pop_back(); return C; } int main() { cin>>a>>b; vector<int>A,B,C; int r=0;//a/b余多少 for(int i=a.size()-1;i>=0;i--) A.push_back(a[i]-'0'); C=div(A,b,r); for(int i=C.size()-1;i>=0;i--) cout<<C[i]; cout<<endl<<r<<endl; }
2.高精度除以高精度:
#include<bits/stdc++.h> using namespace std; const int N=100005; string a; string b; bool cmp(vector<int> &A, vector<int> &B){ if(A.size()!=B.size()) return A.size()>B.size(); for(int i=A.size()-1;i>=0;i--){ if(A[i]!=B[i]) return A[i]>B[i]; } return true; } vector<int> sub(vector<int> &A,vector<int> &B){ vector<int> C; int t=0; for(int i=0;i<A.size()||t;i++){ t = A[i] - t; if(i<B.size()) t -= B[i]; C.push_back((t+10)%10); if(t<0) t = 1; else t = 0; } while(C.size()>1&&C.back()==0) C.pop_back(); return C; } vector<int> div(vector<int> &A, vector<int> &B, vector<int> &r){ vector<int> C; int j = B.size(); r.assign(A.end()-j,A.end()); while(j<=A.size()){ int k=0; while(cmp(r,B)){ vector<int> s = sub(r,B); r.clear(); r.assign(s.begin(),s.end()); k++; } C.push_back(k); if(j<A.size()) r.insert(r.begin(),A[A.size()-j-1]); if(r.size()>1&&r.back()==0) r.pop_back(); j++; } reverse(C.begin(),C.end()); while(C.size()>1&&C.back()==0) C.pop_back(); return C; } int main() { cin>>a>>b; vector<int>A,B,C,r; 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'); C=div(A,B,r); for(int i=C.size()-1;i>=0;i--) cout<<C[i]; cout<<endl; for(int i=r.size()-1;i>=0;i--) printf("%d",r[i]); cout<<endl; }
如果是python玩家:
a=int(input()) b=int(input()) print(a//b) print(a%b)
int128:
__int128 read() { __int128 x = 0, f = 1; char ch = getchar(); while (!isdigit(ch) && ch != '-')ch = getchar(); if (ch == '-')f = -1, ch = getchar(); while (isdigit(ch))x = x * 10 + ch - '0', ch = getchar(); return f * x; }
inline void print(__int128 x) { if (x < 0) { putchar('-'); x = -x; } if (x > 9) print(x / 10); putchar(x % 10 + '0'); }