#if 1 #include <iostream> #include <vector> #include <algorithm> using namespace std; 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; } //非负的A>=B,返回true //A、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> D; for (int i = 0, t = 0; i < A.size() || i < B.size(); i++) { t = A[i] - t; if (i < B.size()) t -= B[i]; D.push_back((t + 10) % 10); if (t >= 0) t = 0; else t = 1; } while (D.size() > 1 && D.back() == 0) D.pop_back(); return D; } vector<int> mul(vector<int>& A, int b) { vector<int> E; for (int i = 0, t = 0; i < A.size() || t; i++) { if(i < A.size()) t += A[i] * b; E.push_back(t % 10); t /= 10; } return E; } vector<int> div(vector<int>& A, int b, int& r) { vector<int> F; r = 0; for (int i = A.size() - 1; i >= 0; i--) { r = r * 10 + A[i]; F.push_back(r / b); r %= b; } reverse(F.begin(), F.end()); while (F.size() > 1 && F.back() == 0) F.pop_back(); return F; } int main() { string a, b; //"123456" cin >> a >> b; vector<int> A, B; for (int i = a.size() - 1; i >= 0; i--) A.push_back(a[i] - '0'); //[6,5,4,3,2,1] for (int i = b.size() - 1; i >= 0; i--) B.push_back(b[i] - '0'); vector<int> C = add(A, B); for (int i = C.size() - 1; i >= 0; i--) printf("%d", C[i]); cout << endl; if (cmp(A, B)) { auto D = sub(A, B); for (int i = D.size() - 1; i >= 0; i--) printf("%d", D[i]); } else { auto D = sub(B, A); printf("-"); for (int i = D.size() - 1; i >= 0; i--) printf("%d", D[i]); } cout << endl; int c; scanf("%d", &c); auto E = mul(A, c); for (int i = E.size() - 1; i >= 0; i--) printf("%d", E[i]); cout << endl; int r; auto F = div(A, c, r); for (int i = F.size() - 1; i >= 0; i--) printf("%d", F[i]); printf("...%d", r); return 0; } #endif
标签:return,高精度,int,四则运算,back,--,算法,vector,size From: https://www.cnblogs.com/karinto/p/17716589.html