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. Python模块——Openpyxl(EXCEL)操作

    一.安装模块 pip install openpyxl 二.文件的操作 2.1文件创建 from openpyxl import Workbook #创建新的excle文件 wk = Workbook ...

  2. 鸟哥的linux私房菜——第四章学习

    ******************第四章学习****************** [热键] 1.Tab键:命令补全:文件补全: 2.Ctrl+c:中断目前指令: 3.Ctrl+d:离开当前文本界面: ...

  3. FZU2105 Digits Count(按位建线段树)题解

    题意: 给出区间与.或.异或\(x\)操作,还有询问区间和. 思路: 因为数比较小,我们给每一位建线段树,这样每次只要更新对应位的答案. 与\(0\)和或\(1\)相当于重置区间,异或\(1\)相当于 ...

  4. 2017.8.11 think list

    递推式与模数不互质,如何利用中国剩余定理综合答案

  5. sdut2879 枚举起点DP

    这个题和乌龟棋之类的DP差不多要学会缩减状态 就是,,我们只需枚举当前这个人是谁,选什么颜色,A用了多少,B用了多少 C用了多少我们就不用枚举了,知道选了多少人,A,B用了多少,你还不知C用了多少么, ...

  6. 2016 JS 笔试题汇总:

    1 1 1 CS&S(中软国际): 1 JavaScript 循环表达式: 2  JavaScript表达式boolean返回值: 3 网页中的事件/HTML 事件属性/JavaScript ...

  7. SQL All In One

    SQL All In One Structured Query Language SQL is an ANSI (American National Standards Institute) stan ...

  8. useful podcast

    useful podcast front end podcast https://shoptalkshow.com https://stackoverflow.blog/podcast/ SoundC ...

  9. scroll tabs

    scroll tabs https://github.com/NervJS/taro-ui/blob/dev/src/components/tabs/index.tsx https://github. ...

  10. Chateau Renice酒庄 :忠于风味,尊重自然

    Chateau Renice酒庄(公司编号:20151318780)凭借优良的葡萄栽培传统.卓越的酿酒技术以及独特风味的葡萄酒,近年来葡萄酒畅销至全球. Chateau Renice酒庄将葡萄酒出口至 ...