高精度算法总结

高精度加法

题目链接:

https://www.acwing.com/problem/content/793/

代码模版:

#include <iostream>
#include <vector>

using namespace std;

// C = A + B
vector<int> add(vector<int> &A, vector<int> &B)
{
    vector<int> C;

    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];
        C.push_back(t % 10);
        t /= 10;
    }

    if (t) C.push_back(1);
    return C;
}

int main()
{
    string a, b;
    vector<int> A, B;

    cin >> a >> b;  // a = "123456"
    for (int i = a.size() - 1; i >= 0; i--) A.push_back(a[i] - '0');  // A = [6, 5, 4, 3, 2, 1]
    for (int i = b.size() - 1; i >= 0; i--) B.push_back(b[i] - '0');

    auto C = add(A, B);

    for (int i = C.size() - 1; i >= 0; i--) printf("%d", C[i]);
    return 0;
}

 

高精度减法

题目链接:

https://www.acwing.com/problem/content/794/

代码模版:

#include <iostream>
#include <vector>

using namespace std;

// 判断是否有 A >= B
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
vector<int> sub(vector<int> &A, vector<int> &B)
{
    vector<int> C;
    for (int i = 0, t = 0; i < A.size(); i++)
    {
        t = A[i] - t;
        if (i < B.size()) t -= B[i];
        C.push_back((t + 10) % 10);
        if (t < 0) t = 1;
        else t = 0;
    }

    while (C.size() > 1 && C.back() == 0) C.pop_back();  // 去掉前导0

    return C;
}

int main()
{
    string a, b;
    vector<int> A, B;

    cin >> a >> b;  // a = "123456"
    for (int i = a.size() - 1; i >= 0; i--) A.push_back(a[i] - '0');  // A = [6, 5, 4, 3, 2, 1]
    for (int i = b.size() - 1; i >= 0; i--) B.push_back(b[i] - '0');

    if (cmp(A, B))
    {
        auto C = sub(A, B);

        for (int i = C.size() - 1; i >= 0; i--) printf("%d", C[i]);
    }
    else
    {
        auto C = sub(B, A);

        printf("-");
        for (int i = C.size() - 1; i >= 0; i--) printf("%d", C[i]);
    }
    return 0;
}

 

高精度乘法(高精度 × 低精度)

题目链接:

https://www.acwing.com/problem/content/795/

代码模版:

#include <iostream>
#include <vector>

using namespace std;

// C = A * b
vector<int> mul(vector<int> &A, int b)
{
    vector<int> C;

    int t = 0;  // 进位
    for (int i = 0; i < A.size() || t; i++)
    {
        if (i < A.size()) t += A[i] * b;
        C.push_back(t % 10);
        t /= 10;
    }

    while (C.size() > 1 && !C.back()) C.pop_back();

    return C;
}

int main()
{
    string a;
    int b;
    vector<int> A;

    cin >> a >> b;
    for (int i = a.size() - 1; i >= 0; i--) A.push_back(a[i] - '0');

    auto C = mul(A, b);

    for (int i = C.size() - 1; i >= 0; i--) printf("%d", C[i]);
    return 0;
}

 

高精度乘法(高精度 × 高精度)

题目链接:

https://www.luogu.com.cn/problem/P1303

代码模版:

#include <iostream>
#include <vector>

using namespace std;

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, t = 0; i < C.size() || t; i++)
    {
        t += C[i];
        if (i >= C.size()) C.push_back(t % 10);
        else C[i] = t % 10;
        t /= 10;
    }

    while (C.size() > 1 && !C.back()) C.pop_back();

    return C;
}

int main()
{
    string a, b;
    vector<int> A, B;

    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 C = mul(A, B);

    for (int i = C.size() - 1; i >= 0; i--) cout << C[i];
    return 0;
}

 

高精度除法

题目链接:

https://www.acwing.com/problem/content/796/

代码模版:

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

// A / b,商是C,余数是r
vector<int> div(vector<int> &A, int b, int &r)  // 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()) C.pop_back();

    return C;
}

int main()
{
    string a;
    int b;
    vector<int> A;

    cin >> a >> b;
    for (int i = a.size() - 1; i >= 0; i--) A.push_back(a[i] - '0');

    int r;
    auto C = div(A, b, r);

    for (int i = C.size() - 1; i >= 0; i--) printf("%d", C[i]);
    cout << endl << r << endl;

    return 0;
}

 

posted @ 2023-11-26 13:49  ykycode  阅读(7)  评论(0编辑  收藏  举报