C++ BigInteger模板
#include <cstdio>
#include <cstring>
#include <string>
#include <iostream>
#include <iomanip>
#include <algorithm>
using namespace std; #define MAXN 9999
#define MAXSIZE 10
#define DLEN 4
const long long MOD = ;
class BigInteger
{
private:
int a[]; //可以控制大数的位数
int len; //大数长度
public:
BigInteger(){ len = ; memset(a, , sizeof(a)); } //构造函数
BigInteger(const int); //将一个int类型的变量转化为大数
BigInteger(const char*); //将一个字符串类型的变量转化为大数
BigInteger(const BigInteger &); //拷贝构造函数
BigInteger &operator=(const BigInteger &); //重载赋值运算符,大数之间进行赋值运算 friend istream& operator>>(istream&, BigInteger&); //重载输入运算符
friend ostream& operator<<(ostream&, BigInteger&); //重载输出运算符 BigInteger operator+(const BigInteger &) const; //重载加法运算符,两个大数之间的相加运算
BigInteger operator-(const BigInteger &) const; //重载减法运算符,两个大数之间的相减运算
BigInteger operator*(const BigInteger &) const; //重载乘法运算符,两个大数之间的相乘运算
BigInteger operator/(const int &) const; //重载除法运算符,大数对一个整数进行相除运算 BigInteger operator^(const int &) const; //大数的n次方运算
long long operator%(const long long &) const; //大数对一个int类型的变量进行取模运算
bool operator>(const BigInteger & T)const; //大数和另一个大数的大小比较
bool operator>(const int & t)const; //大数和一个int类型的变量的大小比较 void print(); //输出大数
};
BigInteger::BigInteger(const int b) //将一个int类型的变量转化为大数
{
int c, d = b;
len = ;
memset(a, , sizeof(a));
while (d > MAXN)
{
c = d - (d / (MAXN + )) * (MAXN + );
d = d / (MAXN + );
a[len++] = c;
}
a[len++] = d;
}
BigInteger::BigInteger(const char*s) //将一个字符串类型的变量转化为大数
{
int t, k, index, l, i;
memset(a, , sizeof(a));
l = strlen(s);
len = l / DLEN;
if (l%DLEN)
len++;
index = ;
for (i = l - ; i >= ; i -= DLEN)
{
t = ;
k = i - DLEN + ;
if (k<)
k = ;
for (int j = k; j <= i; j++)
t = t * + s[j] - '';
a[index++] = t;
}
}
BigInteger::BigInteger(const BigInteger & T) : len(T.len) //拷贝构造函数
{
int i;
memset(a, , sizeof(a));
for (i = ; i < len; i++)
a[i] = T.a[i];
}
BigInteger & BigInteger::operator=(const BigInteger & n) //重载赋值运算符,大数之间进行赋值运算
{
int i;
len = n.len;
memset(a, , sizeof(a));
for (i = ; i < len; i++)
a[i] = n.a[i];
return *this;
}
istream& operator>>(istream & in, BigInteger & b) //重载输入运算符
{
char ch[MAXSIZE * ];
int i = -;
in >> ch;
int l = strlen(ch);
int count = , sum = ;
for (i = l - ; i >= ;)
{
sum = ;
int t = ;
for (int j = ; j< && i >= ; j++, i--, t *= )
{
sum += (ch[i] - '')*t;
}
b.a[count] = sum;
count++;
}
b.len = count++;
return in; }
ostream& operator<<(ostream& out, BigInteger& b) //重载输出运算符
{
int i;
cout << b.a[b.len - ];
for (i = b.len - ; i >= ; i--)
{
cout.width(DLEN);
cout.fill('');
cout << b.a[i];
}
return out;
} BigInteger BigInteger::operator+(const BigInteger & T) const //两个大数之间的相加运算
{
BigInteger t(*this);
int i, big; //位数
big = T.len > len ? T.len : len;
for (i = ; i < big; i++)
{
t.a[i] += T.a[i];
if (t.a[i] > MAXN)
{
t.a[i + ]++;
t.a[i] -= MAXN + ;
}
}
if (t.a[big] != )
t.len = big + ;
else
t.len = big;
return t;
}
BigInteger BigInteger::operator-(const BigInteger & T) const //两个大数之间的相减运算
{
int i, j, big;
bool flag;
BigInteger t1, t2;
if (*this>T)
{
t1 = *this;
t2 = T;
flag = ;
}
else
{
t1 = T;
t2 = *this;
flag = ;
}
big = t1.len;
for (i = ; i < big; i++)
{
if (t1.a[i] < t2.a[i])
{
j = i + ;
while (t1.a[j] == )
j++;
t1.a[j--]--;
while (j > i)
t1.a[j--] += MAXN;
t1.a[i] += MAXN + - t2.a[i];
}
else
t1.a[i] -= t2.a[i];
}
t1.len = big;
while (t1.a[t1.len - ] == && t1.len > )
{
t1.len--;
big--;
}
if (flag)
t1.a[big - ] = - t1.a[big - ];
return t1;
} BigInteger BigInteger::operator*(const BigInteger & T) const //两个大数之间的相乘运算
{
BigInteger ret;
int i, j, up;
int temp, temp1;
for (i = ; i < len; i++)
{
up = ;
for (j = ; j < T.len; j++)
{
temp = a[i] * T.a[j] + ret.a[i + j] + up;
if (temp > MAXN)
{
temp1 = temp - temp / (MAXN + ) * (MAXN + );
up = temp / (MAXN + );
ret.a[i + j] = temp1;
}
else
{
up = ;
ret.a[i + j] = temp;
}
}
if (up != )
ret.a[i + j] = up;
}
ret.len = i + j;
while (ret.a[ret.len - ] == && ret.len > )
ret.len--;
return ret;
}
BigInteger BigInteger::operator/(const int & b) const //大数对一个整数进行相除运算
{
BigInteger ret;
int i, down = ;
for (i = len - ; i >= ; i--)
{
ret.a[i] = (a[i] + down * (MAXN + )) / b;
down = a[i] + down * (MAXN + ) - ret.a[i] * b;
}
ret.len = len;
while (ret.a[ret.len - ] == && ret.len > )
ret.len--;
return ret;
}
long long BigInteger::operator %(const long long & b) const //大数对一个int类型的变量进行取模运算
{
int i;
long long d = ;
for (i = len - ; i >= ; i--)
{
d = ((d * (MAXN + )) % b + (long long)(a[i])) % b;
}
return d;
}
BigInteger BigInteger::operator^(const int & n) const //大数的n次方运算
{
BigInteger t, ret();
int i;
if (n<)
exit(-);
if (n == )
return ;
if (n == )
return *this;
int m = n;
while (m>)
{
t = *this;
for (i = ; i << <= m; i <<= )
{
t = t*t;
}
m -= i;
ret = ret*t;
if (m == )
ret = ret*(*this);
}
return ret;
}
bool BigInteger::operator>(const BigInteger & T) const //大数和另一个大数的大小比较
{
int ln;
if (len > T.len)
return true;
else if (len == T.len)
{
ln = len - ;
while (a[ln] == T.a[ln] && ln >= )
ln--;
if (ln >= && a[ln] > T.a[ln])
return true;
else
return false;
}
else
return false;
}
bool BigInteger::operator >(const int & t) const //大数和一个int类型的变量的大小比较
{
BigInteger b(t);
return *this>b;
} void BigInteger::print() //输出大数
{
int i;
cout << a[len - ];
for (i = len - ; i >= ; i--)
{
cout.width(DLEN);
cout.fill('');
cout << a[i];
}
cout << endl;
}
其实这个也没多大用,大数肯定直接用Java了
C++ BigInteger模板的更多相关文章
- 模板-高精度BigInteger
#include <bits/stdc++.h> using namespace std; struct BigInteger { static const int BASE = 1000 ...
- C++ BigInteger 大整数类模板(转)
#include <deque> #include <vector> #include <iostream> #include <string> #in ...
- 【Java】-BigInteger大数类的使用【超强Java大数模板 总结】
Scanner cin = new Scanner(new BufferedInputStream(System.in)); 这样定义Scanner类的对象读入数据可能会快一些! 参考这个博客继续补充 ...
- 模板——BigInteger
#include <iostream> #include <cstring> #include <string> #include <vector> # ...
- BigInteger
首先上模板(不断更新中...)(根据刘汝佳AOAPCII修改) #include <iostream> #include <sstream> #include <cstd ...
- 高精度模板 Luogu P1932 A+B & A-B & A*B & A/B Problem
P1932 A+B & A-B & A*B & A/B Problem 题目背景 这个题目很新颖吧!!! 题目描述 求A.B的和差积商余! 输入输出格式 输入格式: 两个数两行 ...
- Java 大数、高精度模板
介绍: java中用于操作大数的类主要有两个,一个是BigInteger,代表大整数类用于对大整数进行操作,另一个是BigDecimal,代表高精度类,用于对比较大或精度比较高的浮点型数据进行操作.因 ...
- 大数模板 poj3982
1. 这个模板不是自己写的,转载的别人转载的,还没学完c++的我,想写也没有那能力. 这个模板我用在了POJ的一道题上,传送门--POJ3982 一般大数的题,都可用这个模板解决,仅仅须要改动主函数就 ...
- JAVA高精度模板
刚开始还坚持用C++写高精来着,后来发现JAVA写高精方便太多了,所以也来学习一下JAVA高精度的模板. 参考:https://www.cnblogs.com/imzscilovecode/p/883 ...
随机推荐
- Docker yum 安装
[liwm@Eren ~]$ sudo su[root@Eren liwm]# yum install -y docker 已加载插件:fastestmirror, langpacks, prod ...
- 配置 IntelliJ IDEA VM options
今天在使用maven build flex 项目的时候,build failure ,查看log后发现[ERROR] Java heap space. 原来是内存不够了.需要修改maven的运行时内存 ...
- 【转】一天学会PHP(转)
[转]一天学会PHP(转) 只需要一天,只要你用心去看和学,一定行. - 这里希望大家需要明白一点,这只是在讲如何快速入门,更好的认识PHP!也能初级掌握PHP基础知识!PHP语言博大精深!并不是一两 ...
- Obfuscating computer code to prevent an attack
A method and system for obfuscating computer code of a program to protect it from the adverse effect ...
- JavaScript(14)jQuery(JavaScript 库)
JavaScript 框架(库) JavaScript 高级程序设计(特别是对浏览器差异的复杂处理),通常非常困难也非常耗时.为了应对这些调整,很多的 JavaScript (helper) 库应运而 ...
- Java实现二叉树的创建、递归/非递归遍历
近期复习数据结构中的二叉树的相关问题,在这里整理一下 这里包含: 1.二叉树的先序创建 2.二叉树的递归先序遍历 3.二叉树的非递归先序遍历 4.二叉树的递归中序遍历 5.二叉树的非递归中序遍历 6. ...
- MapReduce中combine、partition、shuffle的作用是什么
http://www.aboutyun.com/thread-8927-1-1.html Mapreduce在hadoop中是一个比較难以的概念.以下须要用心看,然后自己就能总结出来了. 概括: co ...
- DNS SOA NS区别
转自 http://bbs.51cto.com/thread-908637-1.html NS服务器里有两个比较重要的记录.一个叫SOA记录(起始授权机构) 一个叫NS(Name Server)记录( ...
- pandas groupby 分组操作
最一般化的groupby 方法是apply. tips=pd.read_csv('tips.csv') tips[:5] 新生成一列 tips['tip_pct']=tips['tip']/tips[ ...
- SQL Server单表已700w+将普通表转换成分区表1
最近项目中,某个表数据量爆发时增长,单表已700w+,读写性能急剧下降,所以考虑加入分区表以解燃眉之急,后续还是要分表分库,当然这是后话.下面简要说一下将普通表转为分区表的步骤. 一.创建文件组 ...