Java和Pathon可以不用往下看了

C++的基本数据类型中,范围最大的数据类型不同编译器不同,但是最大的整数范围只有[-2^63—2^63-1](对应8个字节所对应的二进制数大小)。但是对于某些需要更大数据的问题来说,C++的基本数据类型就不够用了。因此大整数类应运而生。

大整数类实现的基本思路:使用数组来储存数字的每一位,然后用类似于手写“+-×÷”竖式的过程来实现大整数类的加减乘除运算。注意每一个数在数组中都是高位在后,低位在前这样便于进行运算。

代码如下:

详细的东西都在注释中写出来了

函数的接口:因为是string类型的,所以在初始化的时候为Bright(“1”),注意要加双引号。

 1 #include <iostream>
2 #include <cstring>
3 #include <queue>
4 using namespace std;
5 const int maxn = 100;//根据要求定
6 struct Bright {
7 int len, a[maxn] = { 0 };//数组的初始化很重要
8 string x;
9 Bright(string x = "0")//初始化函数
10 {
11 len = x.length();
12 for (int i = len - 1, j = 1; i >= 0; j++, i--)
13 a[j] = x[i] - '0';
14 while (!a[len] && len > 0)//确定位数
15 len--;
16 }
17 int& operator[](int i)//重新定义[]使程序写起来更为方便,int i 中的i表示[]中的值
18 {
19 return a[i];
20 }
21 void print()//打印出值
22 {
23 for (int i = max(len, 1); i >= 1; i--)
24 cout << a[i];
25 }
26 void zhanpin(int len1)//将数组中所有的数变为一位数(展平)
27 {
28 len = len1;
29 for (int i = 1; i < len; i++)
30 {
31 a[i + 1] += a[i] / 10;
32 a[i] = a[i] % 10;
33 }
34 while (!a[len])//确定位数
35 len--;
36 }
37 };
38
39 Bright operator+(Bright a, Bright b)//高精度+高精度
40 {
41 Bright c;
42 int len = max(a.len, b.len);
43 for (int i = 1; i <= len; i++)
44 c[i] += a[i] + b[i];//计算贡献
45 c.zhanpin(len + 1);
46 return c;
47 }
48
49 Bright operator*(Bright a, int b)//高精度*低精度
50 {
51 Bright c;
52 for (int i = 1; i <= a.len; i++)
53 c[i] = a[i] * b;//计算贡献
54 c.zhanpin(a.len + 11);
55 return c;
56 }
57 Bright operator*(Bright a, Bright b)//高精度*高精度
58 {
59 Bright c;
60 int len = a.len + b.len;
61 for (int i = 1; i <= a.len; i++)
62 for (int j = 1; j <= b.len; j++)
63 c[i + j - 1] += a[i] * b[j];//通过竖式计算可以发现a的第i位以及b的第j位相乘是对c的第i+j-1位有贡献。
64 c.zhanpin(len);
65 return c;
66 }
67
68
69 int main()
70 {
71 string A, B;
72 cin >> A >> B;
73 Bright a(A), b(B), c;
74 c = a * b;//(a+b,a-b)
75 c.print();
76 }

下面附上另一种数组储存,不是string类型储存。

#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
const int maxn = 100;//根据实际设置大小
struct Bright {
int len, a[maxn];
Bright(int x = 0)//初始化函数
{
memset(a, 0, sizeof(a));//这个初始化很重要,不要忘记了
for (len = 1; x; len++)//判断结束是以x的值来判断
a[len] = x % 10, x = x / 10;//在这里就已经倒置了
len--;
}
int& operator[](int i)//重新定义[]使程序写起来更为方便,int i 中的i表示[]中的值
{
return a[i];
}
void print()//打印出值
{
for (int i = max(len, 1); i >= 1; i--)
cout << a[i];
}
void zhanpin(int len1)//将数组中所有的数变为一位数
{
len = len1;
for (int i = 1; i < len; i++)
{
a[i + 1] += a[i] / 10;
a[i] = a[i] % 10;
}
while (!a[len])//确定位数
len--;
}
}; Bright operator+(Bright a, Bright b)//高精度+高精度
{
Bright c;
int len = max(a.len, b.len);
for (int i = 1; i <= len; i++)
c[i] += a[i] + b[i];//计算贡献
c.zhanpin(len + 1);
return c;
} Bright operator*(Bright a, int b)//高精度*低精度
{
Bright c;
for (int i = 1; i <= a.len; i++)
c[i] = a[i] * b;//计算贡献
c.zhanpin(a.len + 11);
return c;
}
Bright operator*(Bright a, Bright b)//高精度*高精度
{
Bright c;
int len = a.len + b.len;
for (int i = 1; i < a.len; i++)
for (int j = 1; j < b.len; j++)
c[i + j - 1] += a[i] * b[j];
c.zhanpin(len);
return c;
}
//main函数(省略)

C++高精度计算(大整数类)的更多相关文章

  1. C# 基于大整数类的RSA算法实现(公钥加密私钥解密,私钥加密公钥解密)

    但是C#自带的RSA算法类RSACryptoServiceProvider只支持公钥加密私钥解密,即数字证书的使用. 所以参考了一些网上的资料写了一个RSA的算法实现.算法实现是基于网上提供的一个大整 ...

  2. N!的阶乘附带简单大整数类的输入输出(暂时没有深入的了解)

    Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N! 我的思路:就想着大整数类去了,才发现自己还不能很好的掌握,其实这是一个大 ...

  3. 大整数类BIGN的设计与实现 C++高精度模板

    首先感谢刘汝佳所著的<算法竞赛入门经典>. 众所周知,C++中储存能力最大的unsigned long long 也是有着一个上限,如果我们想计算非常大的整数时,就不知所措了,所以,我写了 ...

  4. 用Java的大整数类BigInteger来实现大整数的一些运算

    关于BigInteger的构造函数,一般会用到两个: BigInteger(String val); //将指定字符串转换为十进制表示形式: BigInteger(String val,int rad ...

  5. [ C++ 快速高精度模板 ] [ BigN类 ] 大整数类 高精度 模板 BigInt FFT 快速傅里叶变换

    [原创 转载请注明]瞎写的,如果代码有错,或者各位大佬有什么意见建议,望不吝赐教 更新日志: 对于规模较小的整数乘法使用$$O(n^2)$$方法,提高速度 modify()和operator[]的bu ...

  6. C++ BigInteger 大整数类模板(转)

    #include <deque> #include <vector> #include <iostream> #include <string> #in ...

  7. C++大整数类模板

    参考 :http://172.21.85.56/oj/resource/reportdetail?report_id=1678 支持 =.abs().pow().+=.-= *=./=.%=.+.-. ...

  8. 【Java编程】Java中的大整数计算

    在上一篇文章中,我们实现了c语言中的大整数的运算,并且用Miller-Rabin算法实现了对大素数的测试.本来我准备用Java代码实现大整数的运算,查了一下资料发现Java中java.math的Big ...

  9. Ural 1158. Censored! 有限状态自动机+DP+大整数

    Ural1158 看上去很困难的一道题. 原文地址 http://blog.csdn.net/prolightsfxjh/article/details/54729646 题意:给出n个不同的字符,用 ...

随机推荐

  1. 微服务架构学习Day01-SpringBoot入门

    基本概念 SpringBoot的优点: 可以创建独立的Spring应用 SpringBoot嵌入Tomcat,Jetty和Unsertow, 不需要部署war文件 根据需要通过maven获取start ...

  2. Hexo、主题、部署上线

    Hexo.主题.部署上线 安装Hexo git和nodejs安装好后,就可以安装hexo了,你可以先创建一个文件夹MyBlog,用来存放自己的博客文件,然后cd到这个文件夹下(或者在这个文件夹下直接右 ...

  3. woj1009 最短路 The Legend of Valiant Emigration

    title: woj1009 最短路 The Legend of Valiant Emigration date: 2020-03-07 categories: acm tags: [acm,最短路, ...

  4. Leetcode(868)-二进制间距

    给定一个正整数 N,找到并返回 N 的二进制表示中两个连续的 1 之间的最长距离. 如果没有两个连续的 1,返回 0 . 示例 1: 输入:22 输出:2 解释: 22 的二进制是 0b10110 . ...

  5. HDU 4649 Professor Tian(概率DP)题解

    题意:一个表达式,n + 1个数,n个操作,每个操作Oi和数Ai+1对应,给出每个操作Oi和数Ai+1消失的概率,给出最后表达式值得期望.只有| , ^,&三个位操作 思路:显然位操作只对当前 ...

  6. React 17 All In One

    React 17 All In One v17.0.1 https://reactjs.org/blog/2020/10/20/react-v17.html https://reactjs.org/b ...

  7. SMS OTP 表单最佳做法 (短信验证)

    <form action="/verify-otp" method="POST"> <input type="text" ...

  8. 10月份上线的NGK有什么不同之处?

    近日,有小道消息传出公链项目NGK即将在10月上线的消息.各大社区纷纷开始布局,市场中关于NGK项目的消息也变得更多了起来.仅是社区热度这一点,对比之下就已经优于很多项目,那么是否还有其他优势呢?让我 ...

  9. Android 之 EditText

    1.使用EditText 的SetInput的方法设置输入类型: 1 //输入类型为没有指定明确的类型的特殊内容类型 2 editText.setInputType(InputType.TYPE_NU ...

  10. 为什么ElasticSearch比MySQL更适合全文索引

    熟悉 MySQL 的同学一定都知道,MySQL 对于复杂条件查询的支持并不好.MySQL 最多使用一个条件涉及的索引来过滤,然后剩余的条件只能在遍历行过程中进行内存过滤,对这个过程不了解的同学可以先行 ...