高精度算法总结

两盒软妹~` 提交于 2019-12-23 01:17:13

高精度算法总结(C++)

A+a,Aa,Aa,A/aA+a,A-a,A*a,A/a 四种大整数运算

  • 核心:将A的每一位放在数组里,且第0位存放个位数字,即逆序存放

高精度加法

  • 核心:Ai+Bi+tAi + Bi + t
  • 参考题目:LeetCode989
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;

const int N = 10e6 + 10;

// 模板
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]; // 当前为有A[i] 就加上  B[i]同理,得到当前为相加为A[i] + B[i] + t
        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;

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

高精度减法

假设两个正整数相减

  • 核心:AiBitAi-Bi-tAiBi+10tAi-Bi + 10 -t
#include <iostream>
#include <vector>
#include <cstring>
using namespace std;

const int N = 1e5 + 10;

// 判断 A >= B
bool cmp(vector<int>& A, vector<int>& B)
{
    if (A.size() != B.size()) return A.size() > B.size();
    else{
        for (int i = A.size() - 1; i >= 0; i--)  // 位数相同再比较每一位的大小
            if (A[i] != B[i])
                return A[i] > B[i];
    }
    return true;
}


vector<int> _sub(vector<int>& A, vector<int>& B)
{
    vector<int> C;
    int t = 0;
    for (int i = 0; i < A.size(); i++){
        t = A[i] - t;
        if (i < B.size()) t -= B[i]; // 如果B存在
        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;
    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');
    
    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;
}

高精度乘法

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

using namespace std;
const int N = 1e6 + 10;

vector<int> mul(vector<int>& A, int b)
{
    vector<int> C;
    int t = 0;
    for (int i = 0; i < A.size() || t; i++){ // i没循环完或进位没处理完
        if (i < A.size()) t += A[i] * b;

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

    auto C = mul(A, b);

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

    return 0;
}

高精度除法

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

using namespace std;
const int N = 1e6 + 10;

// A / b,商是C,余数是r
vector<int> dev(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');
    int r;
    auto C = dev(A, b, r);
    
    for (int i = C.size() - 1; i >= 0; i--) printf("%d", C[i]);
    cout << endl << r << endl;
    return 0;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!