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. jackson学习之十(终篇):springboot整合(配置类)

    欢迎访问我的GitHub https://github.com/zq2599/blog_demos 内容:所有原创文章分类汇总及配套源码,涉及Java.Docker.Kubernetes.DevOPS ...

  2. codeforces 1010 C. Border【exgcd】

    题目链接:戳这里 学习博客:戳这里 题意:给n种数,n种数取任意个任意组合相加为sum,求sum%k有哪些值. 解题思路: 由exgcd可知(具体用到的是贝祖定理),ax + by = c,满足gcd ...

  3. Spring-cloud-netflix-hystrix

    服务注册中心eureka-server已经搭好,并且SPRING-CLOUD-NETFLIX-EUREKA-CLIENT-APPLICATION提供一个hello服务 畏怯还编写一个eureka-cl ...

  4. HDU 6155 Subsequence Count(矩阵 + DP + 线段树)题解

    题意:01串,操作1:把l r区间的0变1,1变0:操作2:求出l r区间的子序列种数 思路:设DP[i][j]为到i为止以j结尾的种数,假设j为0,那么dp[i][0] = dp[i - 1][1] ...

  5. ZOJ 2563 Long Dominoes(状压DP)题解

    题意:n*m的格子,用1 * 3的矩形正好填满它,矩形不能重叠,问有几种填法 思路:poj2411进阶版.我们可以知道,当连续两行的摆法确定,那么接下来的一行也确定.当第一行还有空时,这时第三行必须要 ...

  6. Prometheus Monitoring Solution

    Prometheus Monitoring Solution 普罗米修斯 https://prometheus.io/ 警报 监控 增强指标和警报 领先 开源监控解决方案 https://promet ...

  7. JS Object Deep Copy & 深拷贝

    JS Object Deep Copy & 深拷贝 针对深度拷贝,需要使用其他方法 JSON.parse(JSON.stringify(obj));,因为 Object.assign() 拷贝 ...

  8. GitHub & GraphQL API

    GitHub & GraphQL API https://gist.github.com/xgqfrms/15559e7545f558d85c5efdea79171a3d refs xgqfr ...

  9. Docker & Node.js

    Docker & Node.js https://nodejs.org/en/docs/guides/nodejs-docker-webapp/ https://docs.docker.com ...

  10. idle & js

    idle & js idle meaning in js https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensi ...