输入: vector<int> A : 数组为{个位,十位,...,最高位}
vector<int> B : 数组为{个位,十位,...,最高位}
输出: vector<int> res: 数组为{个位,十位,...,最高位} 相加后的结果
vector<int>add(vector<int> A, vector<int> B){
vector<int> res;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];
res.push_back(t %10);
t /=10;}if(t >0) res.push_back(t);return res;}
高精度减法
// @brief: vector<int> A 从个位开始存储的整数// @brief: vector<int> B 从个位开始存储的整数// @param: bool// @ret: true: A > B false: A <= B;// // @birth: created by weber on 20240328boolcmp(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];}returnfalse;}
vector<int>sub(vector<int> A, vector<int> B){
vector<int> res;int t =0;for(int i =0; i < A.size(); i ++){
t = A[i]- t;if(i < B.size()) t -= B[i];
res.push_back((t +10)%10);// 是否借位if(t <0) t =1;else t =0;}while(res.back()==0&& res.size()>1) res.pop_back();return res;}
高精度乘法
// @brief: vector<int> A 从个位开始存储的整数// @brief: int b 整数// @param: vector<int>// @ret: vector<int> res 从个位开始存储的整数// @birth: created by weber on 20240328
vector<int>mul(vector<int> A,int b){
vector<int> res;int t =0;for(int i =0; i < A.size()|| t; i ++){if(i < A.size()) t += A[i]* b;
res.push_back(t %10);
t /=10;}while(res.size()>1&&res.back()==0) res.pop_back();return res;}
高精度除法
// @brief: vector<int> A 从个位开始存储的整数// @brief: int b 整数// @brief: int r 表示A/B的余数// @param: vector<int>// @ret: vector<int> res 从个位开始存储的整数// @birth: created by weber on 20240328
vector<int>divide(vector<int> A,int b,int&r){
vector<int> res;for(int i = A.size()-1; i >=0; i --){
r = r *10+ A[i];
res.push_back(r / b);
r = r % b;}reverse(res.begin(), res.end());while(res.size()>1&& res.back()==0) res.pop_back();return res;}