先上模板
高精度加法
cpp
// C = A + B, A >= 0, B >= 0
vector<int> add(vector<int> &a,vector<int> &b){
//c为答案
vector<int> c;
//t为进位
int t=0;
for(int i=0;i<a.size()||i<b.size();i++){
//不超过a的范围添加a[i]
if(i<a.size())t+=a[i];
//不超过b的范围添加b[i]
if(i<b.size())t+=b[i];
//取当前位的答案
c.push_back(t%10);
//是否进位
t/=10;
}
//如果t!=0的话向后添加1
if(t)c.push_back(1);
return c;
}
高精度减法
cpp
// C = A - B, 满足A >= B, A >= 0, B >= 0
vector<int> sub(vector<int> &A, vector<int> &B)
{
//答案
vector<int> C;
//遍历最大的数
for (int i = 0, t = 0; i < A.size(); i ++ )
{
//t为进位
t = A[i] - t;
//不超过B的范围t=A[i]-B[i]-t;
if (i < B.size()) t -= B[i];
//合二为一,取当前位的答案
C.push_back((t + 10) % 10);
//t<0则t=1
if (t < 0) t = 1;
//t>=0则t=0
else t = 0;
}
//去除前导零
while (C.size() > 1 && C.back() == 0) C.pop_back();
return C;
}
高精度比大小(cmp函数)
cpp
//高精度比大小
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;
}
高精度乘低精度
cpp
// C = A * b, A >= 0, b >= 0
vector<int> mul(vector<int> &A, int b)
{
//类似于高精度加法
vector<int> C;
//t为进位
int t = 0;
for (int i = 0; i < A.size() || t; i ++ )
{
//不超过A的范围t=t+A[i]*b
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;
}
高精度乘高精度
cpp
vector<int> mul(vector<int> &A, vector<int> &B) {
vector<int> C(A.size() + B.size() + 7, 0); // 初始化为 0,C的size可以大一点
for (int i = 0; i < A.size(); i++)
for (int j = 0; j < B.size(); j++)
C[i + j] += A[i] * B[j];
int t = 0;
for (int i = 0; i < C.size(); i++) { // i = C.size() - 1时 t 一定小于 10
t += C[i];
C[i] = t % 10;
t /= 10;
}
while (C.size() > 1 && C.back() == 0) C.pop_back(); // 必须要去前导 0,因为最高位很可能是 0
return C;
}
高精度除低精度
cpp
// A / b = C ... r, A >= 0, b > 0
vector<int> div(vector<int> &A, int b, int &r)//高精度A,低精度b,余数r
{
vector<int> C;//答案
r = 0;
for (int i = A.size() - 1; i >= 0; i -- )
{
r = r * 10 + A[i];//补全r>=b
C.push_back(r / b);//取当前位的答案
r %= b;//r%b为下一次计算
}
reverse(C.begin(), C.end());//倒序为答案
while (C.size() > 1 && C.back() == 0) C.pop_back();//去除前导零
return C;
}
高精度除高精度
cpp
vector<int> div(vector<int> &A, vector<int> &B, vector<int> &r) {
vector<int> C;
if (!cmp(A, B)) {
C.push_back(0);
r.assign(A.begin(), A.end());
return C;
}
int j = B.size();
r.assign(A.end() - j, A.end());
while (j <= A.size()) {
int k = 0;
while (cmp(r, B)) {
r = sub(r, B);
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;
}
再上题目
P1932 A+B A-B A*B A/B A%B Problem
最后AC代码
cpp
#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
//高精度比大小
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;
}
//高精度加法
// C = A + B, A >= 0, B >= 0
vector<int> add(vector<int> &a, vector<int> &b) {
//c为答案
vector<int> c;
//t为进位
int t = 0;
for (int i = 0; i < a.size() || i < b.size(); i++) {
//不超过a的范围添加a[i]
if (i < a.size())
t += a[i];
//不超过b的范围添加b[i]
if (i < b.size())
t += b[i];
//取当前位的答案
c.push_back(t % 10);
//是否进位
t /= 10;
}
//如果t!=0的话向后添加1
if (t)
c.push_back(1);
return c;
}
//高精度减法
// C = A - B, 满足A >= B, A >= 0, B >= 0
vector<int> sub(vector<int> &A, vector<int> &B) {
//答案
vector<int> C;
//遍历最大的数
for (int i = 0, t = 0; i < A.size(); i ++ ) {
//t为进位
t = A[i] - t;
//不超过B的范围t=A[i]-B[i]-t;
if (i < B.size())
t -= B[i];
//合二为一,取当前位的答案
C.push_back((t + 10) % 10);
//t<0则t=1
if (t < 0)
t = 1;
//t>=0则t=0
else
t = 0;
}
//去除前导零
while (C.size() > 1 && C.back() == 0)
C.pop_back();
return C;
}
//高精度除法
vector<int> mul(vector<int> &A, vector<int> &B) {
vector<int> C(A.size() + B.size() + 7, 0); // 初始化为 0,C的size可以大一点
for (int i = 0; i < A.size(); i++)
for (int j = 0; j < B.size(); j++)
C[i + j] += A[i] * B[j];
int t = 0;
for (int i = 0; i < C.size(); i++) { // i = C.size() - 1时 t 一定小于 10
t += C[i];
C[i] = t % 10;
t /= 10;
}
while (C.size() > 1 && C.back() == 0)
C.pop_back(); // 必须要去前导 0,因为最高位很可能是 0
return C;
}
vector<int> div(vector<int> &A, vector<int> &B, vector<int> &r) {
vector<int> C;
if (!cmp(A, B)) {
C.push_back(0);
r.assign(A.begin(), A.end());
return C;
}
int j = B.size();
r.assign(A.end() - j, A.end());
while (j <= A.size()) {
int k = 0;
while (cmp(r, B)) {
r = sub(r, B);
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() {
string a, b;
vector<int> A, B, r;
//读入高精度
cin >> 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 C1 = add(A, B);
for (int i = C1.size() - 1; i >= 0; i -- )
printf("%d", C1[i]);
cout << endl;
//高精度减法输出
if (cmp(A, B)) {
auto C2 = sub(A, B);
for (int i = C2.size() - 1; i >= 0; i -- )
printf("%d", C2[i]);
cout << endl;
} else {
auto C3 = sub(B, A);
printf("-");
for (int i = C3.size() - 1; i >= 0; i -- )
printf("%d", C3[i]);
cout << endl;
}
//高精度乘法输出
auto C4 = mul(A, B);
for (int i = C4.size() - 1; i >= 0; i--)
printf("%d", C4[i]);
cout << endl;
//高精度除法输出
auto C5 = div(A, B, r);
for (int i = C5.size() - 1; i >= 0; i--)
printf("%d", C5[i]);
cout << endl;
for (int i = r.size() - 1; i >= 0; i--)
printf("%d", r[i]);
cout << endl;
return 0;
}