高精度加法
题目链接:
https://www.acwing.com/activity/content/problem/content/825/
代码模版:
1 #include <iostream> 2 #include <vector> 3 4 using namespace std; 5 6 // C = A + B 7 vector<int> add(vector<int> &A, vector<int> &B) 8 { 9 vector<int> C; 10 11 int t = 0; // 进位 12 for (int i = 0; i < A.size() || i < B.size(); i++) 13 { 14 if (i < A.size()) t += A[i]; 15 if (i < B.size()) t += B[i]; 16 C.push_back(t % 10); 17 t /= 10; 18 } 19 20 if (t) C.push_back(1); 21 return C; 22 } 23 24 int main() 25 { 26 string a, b; 27 vector<int> A, B; 28 29 cin >> a >> b; // a = "123456" 30 for (int i = a.size() - 1; i >= 0; i--) A.push_back(a[i] - '0'); // A = [6, 5, 4, 3, 2, 1] 31 for (int i = b.size() - 1; i >= 0; i--) B.push_back(b[i] - '0'); 32 33 auto C = add(A, B); 34 35 for (int i = C.size() - 1; i >= 0; i--) printf("%d", C[i]); 36 return 0; 37 }View Code
高精度减法
题目链接:
https://www.acwing.com/activity/content/problem/content/826/
代码模版:
1 #include <iostream> 2 #include <vector> 3 4 using namespace std; 5 6 // 判断是否有 A >= B 7 bool cmp(vector<int> &A, vector<int> &B) 8 { 9 if (A.size() != B.size()) return A.size() > B.size(); 10 for (int i = A.size() - 1; i >= 0; i--) 11 if (A[i] != B[i]) 12 return A[i] > B[i]; 13 return true; 14 } 15 16 // C = A - B 17 vector<int> sub(vector<int> &A, vector<int> &B) 18 { 19 vector<int> C; 20 for (int i = 0, t = 0; i < A.size(); i++) 21 { 22 t = A[i] - t; 23 if (i < B.size()) t -= B[i]; 24 C.push_back((t + 10) % 10); 25 if (t < 0) t = 1; 26 else t = 0; 27 } 28 29 while (C.size() > 1 && C.back() == 0) C.pop_back(); // 去掉前导0 30 31 return C; 32 } 33 34 int main() 35 { 36 string a, b; 37 vector<int> A, B; 38 39 cin >> a >> b; // a = "123456" 40 for (int i = a.size() - 1; i >= 0; i--) A.push_back(a[i] - '0'); // A = [6, 5, 4, 3, 2, 1] 41 for (int i = b.size() - 1; i >= 0; i--) B.push_back(b[i] - '0'); 42 43 if (cmp(A, B)) 44 { 45 auto C = sub(A, B); 46 47 for (int i = C.size() - 1; i >= 0; i--) printf("%d", C[i]); 48 } 49 else 50 { 51 auto C = sub(B, A); 52 53 printf("-"); 54 for (int i = C.size() - 1; i >= 0; i--) printf("%d", C[i]); 55 } 56 return 0; 57 }View Code
高精度乘法
题目链接:
https://www.acwing.com/problem/content/795/
代码模版:
1 #include <iostream> 2 #include <vector> 3 4 using namespace std; 5 6 // C = A * b 7 vector<int> mul(vector<int> &A, int b) 8 { 9 vector<int> C; 10 11 int t = 0; // 进位 12 for (int i = 0; i < A.size() || t; i++) 13 { 14 if (i < A.size()) t += A[i] * b; 15 C.push_back(t % 10); 16 t /= 10; 17 } 18 19 while (C.size() > 1 && !C.back()) C.pop_back(); 20 21 return C; 22 } 23 24 int main() 25 { 26 string a; 27 int b; 28 vector<int> A; 29 30 cin >> a >> b; 31 for (int i = a.size() - 1; i >= 0; i--) A.push_back(a[i] - '0'); 32 33 auto C = mul(A, b); 34 35 for (int i = C.size() - 1; i >= 0; i--) printf("%d", C[i]); 36 return 0; 37 }View Code
高精度除法
题目链接:
https://www.acwing.com/problem/content/796/
代码模版:
1 #include <iostream> 2 #include <vector> 3 #include <algorithm> 4 5 using namespace std; 6 7 // A / b,商是C,余数是r 8 vector<int> div(vector<int> &A, int b, int &r) // r是引用 9 { 10 vector<int> C; // 商 11 r = 0; 12 for (int i = A.size() - 1; i >= 0; i--) 13 { 14 r = r * 10 + A[i]; 15 C.push_back(r / b); 16 r %= b; 17 } 18 19 reverse(C.begin(), C.end()); 20 while (C.size() > 1 && !C.back()) C.pop_back(); 21 22 return C; 23 } 24 25 int main() 26 { 27 string a; 28 int b; 29 vector<int> A; 30 31 cin >> a >> b; 32 for (int i = a.size() - 1; i >= 0; i--) A.push_back(a[i] - '0'); 33 34 int r; 35 auto C = div(A, b, r); 36 37 for (int i = C.size() - 1; i >= 0; i--) printf("%d", C[i]); 38 cout << endl << r << endl; 39 40 return 0; 41 }View Code
标签:总结,10,高精度,int,back,--,算法,vector,size From: https://www.cnblogs.com/ykycode/p/17856904.html