标签:while lse eve cto style vector ret reverse bool
1 // 高精度加法 2 // C = A + B, A >= 0, B >= 0 3 vector<int> add(vector<int> &A, vector<int> &B) 4 { 5 if (A.size() < B.size()) return add(B, A); 6 7 vector<int> C; 8 9 int t = 0; 10 for (int i = 0; i < A.size(); i ++ ) 11 { 12 t += A[i]; 13 if (i < B.size()) t += B[i]; 14 C.push_back(t % 10); 15 t /= 10; 16 } 17 18 if (t) C.push_back(t); 19 return C; 20 } 21 22 23 // 高精度减法 24 // C = A - B, 满足A >= B, A >= 0, B >= 0 25 bool cmp(vector<int> &A, vector<int> &B) { 26 if (A.size() != B.size()) 27 return A.size() > B.size(); 28 for (int i = A.size() - 1; i >= 0; i--) 29 if (A[i] != B[i]) 30 return A[i] > B[i]; 31 return 1; 32 } 33 vector<int> sub(vector<int> &A, vector<int> &B) { //倒序传入 34 if (cmp(A, B)) 35 return sub(A, B); 36 vector<int> C; 37 int t = 0; //表示借位 38 for (int i = 0; i < A.size(); i++) { 39 t = A[i] - t; 40 if (i < B.size()) 41 t = t - B[i]; 42 C.push_back((t + 10) % 10); 43 if (t < 0) 44 t = 1; 45 else 46 t = 0; 47 } 48 while (C.size() > 1 && C.back() == 0) 49 C.pop_back(); 50 return C; 51 } 52 53 54 55 // 高精度乘低精度 56 // C = A * b, A >= 0, b > 0 57 vector<int> mul(vector<int> &A, int b) 58 { 59 vector<int> C; 60 int t = 0; 61 for (int i = 0; i < A.size() || t; i ++ ) 62 { 63 if (i < A.size()) t += A[i] * b; 64 C.push_back(t % 10); 65 t /= 10; 66 } 67 68 return C; 69 } 70 // for(int i = 0;i<A.size();i++){ 71 // t+=A[i]*B; 72 // C.push_back(t%10); 73 // t/=10; 74 // } 75 // while(t){ 76 // C.push_back(t%10); 77 // t/=10; 78 // } 79 80 81 // 高精度除以低精度 82 // A / b = C ... r, A >= 0, b > 0 83 vector<int> div(vector<int> &A, int b, int &r) 84 { 85 vector<int> C; 86 r = 0; 87 for (int i = A.size() - 1; i >= 0; i -- ) 88 { 89 r = r * 10 + A[i]; 90 C.push_back(r / b); 91 r %= b; 92 } 93 reverse(C.begin(), C.end()); 94 while (C.size() > 1 && C.back() == 0) C.pop_back(); 95 return C; 96 } 97 //高精度除法
标签:while lse eve cto style vector ret reverse bool
原文地址:https://www.cnblogs.com/hhyx/p/12405615.html