首页 > 其他分享 >高精度加法(含代码)

高精度加法(含代码)

时间:2023-05-26 21:46:21浏览次数:32  
标签:10 高精度 int MAX 代码 --------------------------- 加法 include

高精度加法

例 1

例如: 1111111111111+9, 列成竖式,

   1 1 1 1 1 1 1 1 1 1 1 1 1
 +                         9
 
 ---------------------------
 

先算个位,

1+9=10,
10, 向十位进1

   1 1 1 1 1 1 1 1 1 1 1 1 1
 +                         9
                         1
 ---------------------------
                           0

接下来, 处理进位
十位: 1+1=2 -> 2

   1 1 1 1 1 1 1 1 1 1 1 1 1
 +                         9
                         1
 ---------------------------
                         2 0

百位: 无进位, 直接照抄. 1 -> 1

   1 1 1 1 1 1 1 1 1 1 1 1 1
 +                         9
                         1
 ---------------------------
                       1 2 0

千位: 1 -> 1

万位: ...

...: ...

最高位: 1 -> 1

最终结果:

   1 1 1 1 1 1 1 1 1 1 1 1 1
 +                         9
                         1
 ---------------------------
   1 1 1 1 1 1 1 1 1 1 1 2 0

所以, 1111111111111+9=1111111111120

例 2

1111111111111+8888888888889,
这个算式变成了高精度+高精度了。

还是列成竖式,

   1 1 1 1 1 1 1 1 1 1 1 1 1
 + 8 8 8 8 8 8 8 8 8 8 8 8 9
 
 ---------------------------
 

先算个位,

1+9=10,
10, 向十位进1

   1 1 1 1 1 1 1 1 1 1 1 1 1
 + 8 8 8 8 8 8 8 8 8 8 8 8 9
                         1
 ---------------------------
                           0

接下来, 算十位。

1+8+1=10,
10, 向百位进1

   1 1 1 1 1 1 1 1 1 1 1 1 1
 + 8 8 8 8 8 8 8 8 8 8 8 8 9
                       1 1
 ---------------------------
                         0 0

千位, 万位...以此类推。

   1 1 1 1 1 1 1 1 1 1 1 1 1
 + 8 8 8 8 8 8 8 8 8 8 8 8 9
                     1 1 1
 ---------------------------
                       0 0 0
   1 1 1 1 1 1 1 1 1 1 1 1 1
 + 8 8 8 8 8 8 8 8 8 8 8 8 9
                   1 1 1 1
 ---------------------------
                     0 0 0 0

......

   1 1 1 1 1 1 1 1 1 1 1 1 1
 + 8 8 8 8 8 8 8 8 8 8 8 8 9
   1 1 1 1 1 1 1 1 1 1 1 1
 ---------------------------
     0 0 0 0 0 0 0 0 0 0 0 0

最高位:
1+8+1=10,
向前一位进1.

   1 1 1 1 1 1 1 1 1 1 1 1 1
 + 8 8 8 8 8 8 8 8 8 8 8 8 9
 1 1 1 1 1 1 1 1 1 1 1 1 1
 ---------------------------
   0 0 0 0 0 0 0 0 0 0 0 0 0
   1 1 1 1 1 1 1 1 1 1 1 1 1
 + 8 8 8 8 8 8 8 8 8 8 8 8 9
 1 1 1 1 1 1 1 1 1 1 1 1 1
 ---------------------------
 1 0 0 0 0 0 0 0 0 0 0 0 0 0

所以,

1111111111111+8888888888889=10000000000000=1013

   1 1 1 1 1 1 1 1 1 1 1 1 1
 + 8 8 8 8 8 8 8 8 8 8 8 8 9
 1 1 1 1 1 1 1 1 1 1 1 1 1
 ---------------------------
 1 0 0 0 0 0 0 0 0 0 0 0 0 0

代码

  1. 首先导入头文件。
// 1. Import libraries
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <cstring>
#include <cassert>
#include <vector>
// Import namespace 'std'
using namespace std;
  1. 初始化变量。
// 2. Initialize variables
const int MAX = 1000;
char s[MAX+1];
int a[MAX+1], b[MAX+1],c[MAX+2];
  1. 定义主函数main()
// 3. Define Main Function
int main(int argc, char **argv) {
    for (int i=0;i<1001;i++) {
        s[i]='0';
        a[i]=0, b[i]=0, c[i]=0;
    } // If you use global variables, this initialization does not need used.

这里如果使用全局变量,这个初始化可以不用。
4. 输入(包含了处理)变量

    // 4. Input and process variables
    scanf("%s",s+1);
    int lena = strlen(s+1);

    for (int i=1;i<=lena+1;i++)
        a[i] = s[lena-i+1] - '0';

    scanf("%s",s+1);
    int lenb = strlen(s+1);
    for (int i=1;i<=lenb+1;i++)
        b[i] = s[lenb-i+1] - '0';

    int lenc = (lena>lenb)?lena:lenb;
  1. 真正的高精度计算到这里才开始。计算代码
    // 5. Calculate
    for (int i=1;i<=101;i++)
        c[i] = 0;
    for (int i=1;i<=lenc;i++) {
        // c[i] = a[i] + b[i]; <-- Wrong! 
        c[i] = a[i] + b[i] + c[i]; // <-- Correct
        // c[i] += a[i] + b[i]; <-- Plan B, Correct too
        // 处理进位
        c[i+1] = c[i] / 10;
        c[i] = c[i] % 10;
    }
    // 处理 overflow
    if (c[lenc+1]>0)
        lenc++;
  1. 输出
    // 6. Output
    for (int i=lenc;i>=1;i--)
        printf("%d", c[i]);
    return 0;
}

附: 完整代码

// 1. Import libraries
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <cstring>
#include <cassert>
#include <vector>
// Import namespace 'std'
using namespace std;

// 2. Initialize variables
const int MAX = 1000;
char s[MAX+1];
int a[MAX+1], b[MAX+1],c[MAX+2];


// 3. Define Main Function
int main(int argc, char **argv) {
    for (int i=0;i<1001;i++) {
        s[i]='0';
        a[i]=0, b[i]=0, c[i]=0;
    } // If you use global variables, this initialization does not need used.
	
    // 4. Input and process variables
    scanf("%s",s+1);
    int lena = strlen(s+1);

    for (int i=1;i<=lena+1;i++)
        a[i] = s[lena-i+1] - '0';

    scanf("%s",s+1);
    int lenb = strlen(s+1);
    for (int i=1;i<=lenb+1;i++)
        b[i] = s[lenb-i+1] - '0';

    int lenc = (lena>lenb)?lena:lenb;

    // 5. Calculate
    for (int i=1;i<=101;i++)
        c[i] = 0;
    for (int i=1;i<=lenc;i++) {
        // c[i] = a[i] + b[i]; <-- Wrong! 
        c[i] = a[i] + b[i] + c[i]; // <-- Correct
        // c[i] += a[i] + b[i]; <-- Plan B, Correct too
        // 处理进位
        c[i+1] = c[i] / 10;
        c[i] = c[i] % 10;
    }
    // 处理 overflow
    if (c[lenc+1]>0)
        lenc++;

    // 6. Output
    for (int i=lenc;i>=1;i--)
    printf("%d", c[i]);
    return 0;
}
复制之前看我一下 `Control`, 缩写为Ctrl, 扩写为唱,跳,rap,篮球。 你还敢Ctrl+C吗?

[========]

[========]

[========]

[========]

标签:10,高精度,int,MAX,代码,---------------------------,加法,include
From: https://www.cnblogs.com/Py-Study0/p/High-precision_addition-114514.html

相关文章

  • Python 使用Seaborn画图,代码正常不出图
    不得不说,这个库画出来的图也太好看了。目前还没有很懂这个库,但是需要注意的是我在用完plt之后引入这个,图片一直出不来真的困扰我很久啊。终于找到了救命的办法参考链接:https://blog.csdn.net/qq_56039091/article/details/124024286......
  • [ESP] ESP-IDF WiFi配网(SoftAP+HTTPD)代码备注
    ESP-IDFWiFi配网(SoftAP+HTTPD)代码备注主要是为了方便了解实际代码层面的配网协议是什么样的,所以文档记录的基本上是代码片段。ESP-IDF的版本是V5.0使用SoftAP传输时,为了允许服务发现,必须在开始配置之前初始化mDNS。protocomm_security一共有3个选项,这里只说了protoco......
  • 一些源代码管理工具介绍
    首先,是源代码管理工具能为我们做什么。源代码管理工具的部分作用如下:1.能够追踪一个项目从诞生到一直定案的过程2.记录一个项目所有内容的变化3.方便查阅特定版本的修订情况4.源代码管理工具可以判断每一行代码里面每一个字符是谁写的。而我们使用源代码管理工具,1.由于使用简单,......
  • 源代码管理工具--GitHub
    一.GitHub简介Github是一个基于Git的代码托管平台,主要为开发者提供基于Git仓库的版本托管服务,并提供一个web界面。其由ChrisWanstrath,PJHyett与TomPreston-Werner三位开发者在2008年4月创办。总部位于美国旧金山。使用上,付费用户可以建私人仓库,免费用户只能使用公共仓库,也......
  • 这段代码会抛出NPE,你造吗?----封装AssertUtil来友好地利用断言
    运行下面代码,会抛出NPE。你知道为什么吗?importcn.hutool.core.lang.Assert;publicclassTestMain{publicstaticvoidmain(String[]args){MyClassmyClass=newMyClass();Assert.isTrue(myClass.myProperty==0);}privatestati......
  • 解决方案 | 如何解决subprocess.Popen(cmd)代码中含有空格路径的问题?
     一、背景因为在python中需要用到subprocess.Popen(cmd),其中cmd由一堆连接的字符串构成:譬如,xxx.exeinputdiroutputdir-arg1-arg2(具体例子:1.exe C:\Users\Administrator\Desktop\myoutput -arg1-arg2 )1.exeC:\Users\Administrator\Desktop\新建文件夹C:\Users\A......
  • 一份采用单例模式编写,可读取配置文件的代码
    Confaccess.h#ifndef__CONFACCESS_H__#define__CONFACCESS_H__#include<pthread.h>#include<stdlib.h>#include<string>#include<map>classCConfAccess{public:staticCConfAccess*getInstance(){......
  • 【敲敲云】免费的零代码产品 — 应用创建与设置
    敲敲云可以创建不同的应用,每一个应用我们可以看做一个系统,例如销售系统、财务系统等等。下面我们来看看如何创建应用吧。应用的基础操作:应用的基础操作包含创建应用、修改应用、退出/删除应用、排序应用、维护应用、应用回收站1、新建应用第一种方式:选择需要新建应用的组......
  • Java大文件分片上传/多线程上传源代码
    ​ 这里只写后端的代码,基本的思想就是,前端将文件分片,然后每次访问上传接口的时候,向后端传入参数:当前为第几块文件,和分片总数下面直接贴代码吧,一些难懂的我大部分都加上注释了:上传文件实体类:看得出来,实体类中已经有很多我们需要的功能了,还有实用的属性。如MD5秒传的信息。pub......
  • arduino esp32 ds18b20 实例代码
    #include<OneWire.h>#include<DallasTemperature.h>//引脚定义#defineONE_WIRE_BUS15//库引用实例OneWireoneWire(ONE_WIRE_BUS);DallasTemperaturesensors(&oneWire);voidsetup(void){Serial.begin(9600);sensors.begin();}voidloop(vo......