首页 > 其他分享 >高精度加减乘除

高精度加减乘除

时间:2023-10-18 19:11:28浏览次数:27  
标签:高精度 int vector ans n1 n2 size 加减乘除

大整数的高精度运算

加法

#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;
}

标签:高精度,int,vector,ans,n1,n2,size,加减乘除
From: https://www.cnblogs.com/orangecodelog/p/17773109.html

相关文章

  • 请完善课上的口算题卡代码,实现重复题目的检测、题目数字范围、加减乘除算式的参数化等
    importjava.util.HashSet;importjava.util.Random;importjava.util.Set;publicclassMathQuizGenerator{  publicstaticvoidmain(String[]args){    intnumberOfQuestions=10;//设定生成题目的数量    intminNumber=1;//题目数字的最小值 ......
  • JS 数字类型的加减乘除, 四舍五入保持精度
    Number.prototype.toFixed=function(d=0){ letchangeNum=this+''//把数字转为字符串 if(changeNum.indexOf('-')!=-1){//判断是否是负数 changeNum=Math.abs(Number(changeNum))} changeNum=(Math.round(Number(changeNum)*Math.......
  • 高精度除法
    一、算法描述高精度除法和乘法讨论的一样,都是一个大整数和一个小整数之间的运算。算法思路根据小学除法一样,我们还是模拟这个过程。注意这里遍历\(A\)数组的时候要按照我们读数字的顺序,也就是从数组尾部到头部遍历。每一位的计算过程应该是,上一轮的余数\(r\)乘\(10\)之......
  • RTK高精度定位安全帽-UWB室内定位/GPS定位智能安全帽技术方案
    高精度RTK定位安全帽是一种综合性解决方案,结合了先进的GPS定位技术、物联网技术、移动数据传输和智能执法管理。相较于传统的定位安全帽,它提供了更高的定位精度、更强大的数据处理能力和更全面的施工过程记录,对公共安全领域有着实质性的积极影响。该安全帽采用厘米级RTK定......
  • 高精度乘法
    一、算法描述高精度加减法讨论的是两个大整数之间的运算。而这里高精度乘除法讨论的是一个大整数和一个小整数之间的关系。算法思路:还是模拟小学的乘法列竖式,只不过此时不太一样,原本的列竖式是一位一位的乘,这里需要改变一下思路。这里直接把小整数当成一个数,所乘的数直接......
  • 高精度算法
    1.高精度加法这个比较简单一些,主要是考虑满10进位的问题,直接写代码就可以。(若数字很大的话,不太好运算,所以将数字转化成字符串的形式输入)#include<iostream>usingnamespacestd;constintN=100010;intA[N],B[N],C[N];intAdd(inta[],intb[],intc[],intcnt)......
  • 高精度减法
    一、算法描述要实现两个高精度数的减法,和高精度加法一样都是模拟竖式计算的过程,主要就是解决以下两个问题。谁大谁小?由于这两个数字都很大,但是不知道谁更大,所以要先判断哪个数更大,思路如下:判断这两个数谁的位数更大,位数更大的自然更大。如果位数不相同,从最高位开始往低位......
  • 高精度
    用处当我们做一些(SB)题时,会发现答案有可能会爆longlong,那么这时候就要用高精度了实现高精度其实就是用一个数组存数位来表示这个数,然后模拟加,减,乘的过程来算出答案高精度板子(加法)for(inti=10000;i>=1;i--){c[i]+=(a[i]+b[i])%10;c[i+1]+=(a[i]+b......
  • 高精度加法
    一、算法描述高精度问题是指两个数字非常大,超过了int,甚至longlong的范围,数字的位数甚至能达到\(10^5\),那么如果要实现这样两个大数字的运算,需要解决以下两个问题:如何存储?这样的两个数字相加是不可能用普通类型来存储的,所以我们第一个要解决的问题就是如何存储高精度数。首......
  • 工程实践之高精度计算器
    说明本代码使用easyx绘制计算器界面,同时使用高精度算法实现大整数的连续运算前提准备需要安装easyx图形库,具体安装和使用流程请点击链接查看代码怎么用需要新建一个工程,向工程中添加头文件和源代码,或者先把所有头文件都提取出来放到一个新的cpp(虽然是c语言,但是easyx底层实现用......