首页 > 其他分享 >高精度HighAccuracy_acwing.cpp

高精度HighAccuracy_acwing.cpp

时间:2022-10-29 16:00:18浏览次数:106  
标签:reverse int back HighAccuracy vector cpp acwing size

 文章:

        力扣模板:字符串相加 - 字符串相加 - 力扣(LeetCode)

        acwing模板:常用代码模板1——基础算法 - AcWing

 

例题:

        P1009 [NOIP1998 普及组] 阶乘之和 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

 

笔记:HighAccuracy_acwing.cpp

        说明:参考acwing的模板,但是和acwing模板不完全相同

#include<bits/stdc++.h>
using namespace std;

//HighAccuracy_acwing.cpp

/*
说明:
参考acwing的模板:https://www.acwing.com/blog/content/277/
但是和acwing模板不完全相同
*/

// C = A + B, A >= 0, B >= 0
//这里不加&,就可以避免改变了原来的a,b数组,使得a,b数组可以重复利用
vector<int> add(vector<int> A, vector<int> B){  //正序输入两个vector类型的“数”
    if (A.size() < B.size()) return add(B, A);  //使得A数组是最大的
    //如果未反转,在if下面加reverse,不能在if上面加reverse,否者二次反转,等于没有反转
    
    reverse(A.begin(),A.end());  //反转数组,变成个十百千万···
    reverse(B.begin(),B.end());
    vector<int> C;  //初始化答案数组
    int t = 0;
    for (int i = 0; i < A.size(); i ++ ){
        t += A[i];
        if (i < B.size()) t += B[i];  //如果B还有数字
        C.push_back(t % 10);
        t /= 10;
    }
    if (t) C.push_back(t);//最后一次加法可能还有进位的数,把最高位补上
    return C;
}

string add(string a, string b){
    if(a.size()<b.size()) return add(b,a);
    reverse(a.begin(),a.end());
    reverse(b.begin(),b.end());
    int t = 0;
    string c;
    for(int i = 0; i<a.size(); i++){
        t += a[i] - '0';
        if(i<b.size()) t+=b[i] - '0';
        c.push_back(t%10 + '0');
        t /= 10;
    }
    if(t) c.push_back(t + '0');
    reverse(c.begin(), c.end());
    return c;
}

// C = A - B, 满足A >= B, A >= 0, B >= 0
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();
    return C;
}

/*高精度乘低精度 —— 模板题*/
// C = A * b, A >= 0, b >= 0
vector<int> mul(vector<int> A, int b){  //输入正序的高精度A和低精度b
    vector<int> C;  //初始化答案数组
    reverse(A.begin(),A.end());  //反转数组,变成个十百千万···
    int t = 0;
    for (int i = 0; i < A.size() || t; i++ ){  //A数组还有数 或 t还有数
        if (i < A.size()) t += A[i] * b;  //如果 A数组还有数
        C.push_back(t % 10);
        t /= 10;
    }
    while (C.size() > 1 && C.back() == 0) C.pop_back();  //末尾可能是0,都要去掉(我感觉是多余的,不可能有这种情况)(难道是处理b==0?)
    reverse(C.begin(),C.end());  //反转数组,恢复正序
    return C;
}

string mul(string a, int b){
    reverse(a.begin(), a.end());
    string c;
    int t = 0;
    for(int i = 0; i<a.size() || t; i++){
        if(i<a.size()) t += (a[i] - '0') * b;
        c.push_back(t%10 + '0');
        t /= 10;
    }
    while(c.size()>1 && c.back() == 0)c.pop_back();
    reverse(c.begin(), c.end());
    return c;
}

int main(){
    //加法
    vector<int> addint1 = {1,2,3,4,5,6,7,8,9};
    vector<int> addint2 = {9,8,7,6,5,4,3,2,1,0};
    vector<int> addint3 = add(addint1,addint2);
    for(int i = 0;i<addint3.size(); i++) cout<<addint3[i];
    printf("\n");

    //加法
    string addstr1 = {"123456789"};
    string addstr2 = {"9876543210"};
    string addstr3 = add(addstr1,addstr2);
    cout<< addstr3 <<endl;

    //乘法
    vector<int> mulint1 = {1,2,3,4,5,6,7,8,9};
    int mulint2 = 100000;
    vector<int> mulint3 = mul(mulint1,mulint2);
    for(int i = 0;i<mulint3.size(); i++) cout<<mulint3[i];
    printf("\n");

    //乘法
    string mulstr1 = "123456789";
    int mulstr2 = 100000;
    cout<<mul(mulstr1,mulstr2)<<endl;

    return 0;
}

 

标签:reverse,int,back,HighAccuracy,vector,cpp,acwing,size
From: https://www.cnblogs.com/--OvO--/p/16838921.html

相关文章

  • AcWing 93. 递归实现组合型枚举
    #include<iostream>#include<cstring>#include<algorithm>usingnamespacestd;constintN=30;//多开几个,防止边界越界intn,m;intway[N];//表示方案//......
  • VSCode-配置c_cpp环境
    VSCode-配置c_cpp环境mac下VSCode配置cpp环境记录:一.环境macOS:10.14VSCode:1.44.2Xcode:11.3.1(会用到Xcode的工具链,如lldb、clang)二.安装插件编辑、调试C/C++......
  • AcWing 94. 递归实现排列型枚举
    #include<iostream>#include<cstring>#include<cstdio>#include<algorithm>usingnamespacestd;constintN=10;intn;intpath[N];//0表示没有放数,1-n表示......
  • AcWing 92. 递归实现指数型枚举
    题解1:#include<iostream>#include<cstring>#include<algorithm>usingnamespacestd;constintN=16;intn;boolst[N];//false不选,true选voiddfs(intu){......
  • AcWing1069.凸多边形的划分(区间DP)
    SLOJP2067.三角剖分问题AcWing1069.凸多边形的划分(区间DP)题目描述给定由N顶点组成的凸多边形每个顶点具有权值将凸N边形剖分成N-2个三角形求N-2个三角形......
  • VScode配置cpp运行调试环境
    1.说明本文基于的操作系统环境为windows10,VScode版本为1.72.2。2.VScode安装及配置在VScode官网(https://code.visualstudio.com)下载安装包,按照提示一路安装下来即可。......
  • Acwing 4708 . 立方体(三维bfs)
    https://www.acwing.com/problem/content/4711/题目没什么难度,但是就是三维有些东西不经常定义记不住。写个题解记录一下吧Acwing1096.地牢大师https://www.acwing.co......
  • ACWing 3549 -- dp&&滚动数组
    题目描述最长非递减子序列思路Reference代码......
  • ACWing 3548 -- 离线算法
    题目描述双端队列思路参考代码......
  • Acwing 788.逆序对的数量
    #include<bits/stdc++.h>usingnamespacestd;constintN=1e+5;inta[N],tmp[N];typedeflonglongll;#注意题目条件llmerge_sort(intq[],intl,intr){......