高精度加减乘除

发布时间 2023-10-18 19:11:10作者: 橙之夏

大整数的高精度运算

加法

#include <bits/stdc++.h>

using namespace std;

vector<int>  big_num_add(vector<int> n1,vector<int> n2)
{
    vector<int> ans;
    int t=0;
    for(int i=0;i<n1.size()||i<n2.size();i++)
    {
        if(i<n1.size())
            t+=n1[i];
        if (i<n2.size())
            t+=n2[i];
        ans.push_back(t%10);
        t /= 10;
    }
    if(t) ans.push_back(1);
    return ans;
}

int main() {
    string s1,s2;
    cin >> s1 >> s2;
    vector<int> n1,n2;
    for(int i=s1.size()-1;i>=0;i--) n1.push_back(s1[i]^48);
    for(int i=s2.size()-1;i>=0;i--) n2.push_back(s2[i]^48);
    auto ans = big_num_add(n1,n2);
    for(int i=ans.size()-1;i>=0;i--) cout<<ans[i];
}

减法

#include <bits/stdc++.h>

using namespace std;

//Comparing two vectors of integers
bool cmp(vector<int> n1,vector<int> n2)
{
    //Checking if the size of the vectors are different
    if(n1.size()^n2.size()) return n1.size() > n2.size();
    //Checking if any element of the vectors are different
    for(int i = n1.size()-1;i>=0;i--)
        if(n1[i]^n2[i]) return n1[i]>n2[i];
    //If all elements are same, return true
    return true;

}

//Subtracting two vectors of integers
vector<int>  big_num_sub(vector<int> n1,vector<int> n2)
{
    //Creating an empty vector to store the result
    vector<int> ans;
    //Initializing the carry
    int t = 0;
    //Iterating through the vectors
    for (int i = 0; i < n1.size(); ++i) {
        //Checking if the size of the vectors are same
        if(i<n2.size())
            //Subtracting the corresponding elements of the vectors
            t = n1[i] - n2[i] - t;
        else
            //If the size of the vectors are different, subtracting the corresponding element of the first vector
            t = n1[i] - t;
        //Adding the carry to the result
        ans.push_back((t+10)%10);
        //Updating the carry
        if (t<0) t = 1;
        else t = 0;
    }
    //Removing the leading zeros
    while (ans.size() > 1 && ans.back() == 0) ans.pop_back();
    //Returning the result
    return ans;
}

int main() {
    //Reading the input
    string s1,s2;
    cin >> s1 >> s2;
    //Creating the vectors
    vector<int> n1,n2;
    //Iterating through the input and converting it to integers
    for(int i=s1.size()-1;i>=0;i--) n1.push_back(s1[i]^48);
    for(int i=s2.size()-1;i>=0;i--) n2.push_back(s2[i]^48);
    //Checking if the size of the vectors are different
    if(cmp(n1,n2))
    {
        //Subtracting the vectors
        auto ans = big_num_sub(n1,n2);
        //Iterating through the result and printing it
        for(int i=ans.size()-1;i>=0;i--) cout<<ans[i];
    }
    else {
        //Printing a negative sign
        cout<<"-";
        //Subtracting the vectors
        auto ans = big_num_sub(n2, n1);
        //Iterating through the result and printing it
        for(int i=ans.size()-1;i>=0;i--) cout<<ans[i];
    }
}

乘法

#include <bits/stdc++.h>

using namespace std;

vector<int>  big_num_mul(vector<int> n1,int n2)
{
    vector<int> ans;
    int t=0;
    for(int i=0;i<n1.size()||t;i++)
    {
        if(i<n1.size()) t += n1[i]*n2;
        ans.push_back(t%10);
        t /= 10;
    }
    while (ans.size() > 1 && ans.back() == 0) ans.pop_back();
    return ans;
}

int main() {
    string s1;
    int n2;
    cin >> s1 >> n2;
    vector<int> n1;
    for(int i=s1.size()-1;i>=0;i--) n1.push_back(s1[i]^48);
    auto ans = big_num_mul(n1,n2);
    for(int i=ans.size()-1;i>=0;i--) cout<<ans[i];
}

除法

#include <bits/stdc++.h>

using namespace std;

vector<int>  big_num_div(vector<int> n1,int n2,int & r)
{
    vector<int> ans;
    r = 0;
    for(int i=n1.size()-1;i>=0;i--)
    {
        r = r*10 + n1[i];
        ans.push_back(r/n2);
        r = r % n2;
    }
    reverse(ans.begin(),ans.end());
    while (ans.size() > 1 && ans.back() == 0) ans.pop_back();
    return ans;
}

int main() {
    string s1;
    int n2;
    cin >> s1 >> n2;
    vector<int> n1;
    for(int i=s1.size()-1;i>=0;i--) n1.push_back(s1[i]^48);
    int r;
    auto ans = big_num_div(n1,n2,r);
    for(int i=ans.size()-1;i>=0;i--) cout<<ans[i];
    cout<<endl<<r<<endl;
}