高精度

原理

乘法

除法

代码

加减

#include <iostream>
#include <vector>
using namespace std;

// C = A + B,其中A,B ≥ 0
vector<int> add (vector<int>& A, vector<int>& B) {
    if (A.size() < B.size()) return add(B, A);
    vector<int> C;
    int carry = 0;
    for (int i = 0; i < A.size(); ++ i) {
        carry += A[i];
        if (i < B.size()) carry += B[i];
        C.push_back(carry % 10);
        carry /= 10;
    }
    if (carry) C.push_back(carry);
    return C;
}
// C = A - B,其中A ≥ B ≥ 0
vector<int> sub (vector<int>& A, vector<int>& B) {
    vector<int> C;
    int borrow = 0;
    for (int i = 0; i < A.size(); ++ i) {
        borrow = A[i] - borrow;
        if (i < B.size()) borrow -= B[i];
        C.push_back((borrow + 10) % 10);
        borrow = borrow < 0 ? 1 : 0;
    }
    while (C.size() > 1 && C.back() == 0) C.pop_back();
    return C;
}
vector<int> max (vector<int> A, vector<int> B) {
    if (A.size() > B.size()) return A;
    if (A.size() < B.size()) return B;
    if (vector<int>(A.rbegin(), A.rend()) > vector<int>(B.rbegin(), B.rend())) return A;
    return B;
}

int main () {
    string a, b;
    cin >> a >> b;

    vector<int> 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');
    vector<int> C = add(A, B); // sub(A, B)
    for (int i = C.size() - 1; i >= 0; -- i) printf("%d", C[i]);
    return 0;
}

乘除

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

// C = A * b,其中A ≥ 0,b > 0
vector<int> mul (vector<int>& A, int b) {
    vector<int> C;
    int carry = 0;
    for (int i = 0; i < A.size() || carry != 0; ++ i) {
        if (i < A.size()) carry += A[i] * b;
        C.push_back(carry % 10);
        carry /= 10;
    }
    return C;
}
// C = A * B,其中A,B ≥ 0
vector<int> mul (vector<int>& A, vector<int>& B) {
    vector<int> C(A.size() + B.size());
    for (int i = 0; i < A.size(); ++ i)
        for (int j = 0; j < B.size(); ++ j)
            C[i + j] += A[i] * B[j];
    for (int i = 0; i < C.size(); ++ i) {
        C[i + 1] += C[i] / 10;
        C[i] %= 10;
    }
    while (C.size() > 1 && C.back() == 0) C.pop_back();
    return C;
}
// A / b = C ... r,其中A ≥ 0,b > 0
vector<int> div (vector<int>& A, int b, int& r) {
    vector<int> C;
    r = 0;
    for (int i = A.size() - 1; i >= 0; -- i) {
        r = r * 10 + A[i];
        C.push_back(r / b);
        r %= b;
    }
    reverse(C.begin(), C.end());
    while (C.size() > 1 && C.back() == 0) C.pop_back();
    return C;
}

int main () {
    // string a;
    // int b;
    // cin >> a >> b;
    // vector<int> A;
    // for (int i = a.size() - 1; i >= 0; -- i) A.push_back(a[i] - '0');
    // vector<int> C = mul(A, b);
    // for (int i = C.size() - 1; i >= 0; -- i) cout << C[i];

    // string a, b;
    // cin >> a >> b;
    // vector<int> 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');
    // vector<int> C = mul(A, B);
    // for (int i = C.size() - 1; i >= 0; -- i) printf("%d", C[i]);

    string a;
    int b, r;
    cin >> a >> b;
    vector<int> A;
    for (int i = a.size() - 1; i >= 0; -- i) A.push_back(a[i] - '0');
    vector<int> C = div(A, b, r);
    for (int i = C.size() - 1; i >= 0; -- i) cout << C[i];
    cout << endl << r << endl;
    return 0;
}
最后修改于: