当前位置:网站首页>大整数类实现阶乘
大整数类实现阶乘
2022-07-07 07:33:00 【昨夜太平长安_】
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn = 10000;
struct BigInt
{
BigInt(int x = 0)
{
memset(arr, 0, sizeof(arr));//初始化全为0
for (len = 1; x; len++)
{
arr[len] = x % 10;//对x逆序保存
x /= 10;
}
len--;
}
int& operator[](int i)
{
return arr[i];//用x[i]代替x.arr[i]
}
void flatten(int L)//把不是一位数的位处理成一位数,故称展平,L要不小于有效长度
{
len = L;
for (int i = 1; i <= len; ++i)
{
arr[i + 1] += arr[i] / 10;
arr[i] %= 10;
}
while (!arr[len])//消除前置0
{
len--;
}
}
void print()//反向输出
{
for (int i = max(1, len); i >= 1; --i)
{
cout << arr[i];
}
cout << endl;
}
int len;
int arr[maxn];
};
BigInt operator+(BigInt &a,BigInt &b)
{
BigInt c;
int len = max(a.len, b.len);
for (int i = 1; i <= len; ++i)
{
c[i] += a[i] + b[i];
}
c.flatten(len + 1);//最大长度不超过len+1
return c;
}
BigInt operator*(BigInt& a, BigInt& b)
{
BigInt 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];//a[i] * b[j]的结果产生在i+j-1位上
}
}
c.flatten(len );//最大长度不超过len+1
return c;
}
BigInt operator*(BigInt& a, int& b)
{
BigInt c;
int len = a.len;
for (int i = 1; i <= len; ++i)
{
c[i] += a[i] *b;
}
c.flatten(len + 11);//int类型最长10位,所以可以这样展平
return c;
}
int main()
{
int n;
cin >> n;
BigInt fac(1);
BigInt ans(0);
for (int i = 1; i <= n; ++i)
{
fac = fac * i;
ans = ans + fac;
}
ans.print();
return 0;
}
边栏推荐
- Write it into the SR table in the way of flinksql. It is found that the data to be deleted has not been deleted. Refer to the document https://do
- Win10 installation vs2015
- Why does the starting service report an error when installing MySQL? (operating system Windows)
- Interface test
- 【学习笔记-李宏毅】GAN(生成对抗网络)全系列(一)
- The Himalaya web version will pop up after each pause. It is recommended to download the client solution
- MySQL can connect locally through localhost or 127, but cannot connect through intranet IP (for example, Navicat connection reports an error of 1045 access denied for use...)
- ORM模型--关联字段,抽象模型类
- Postman tutorial - scripting
- Please ask me a question. I started a synchronization task with SQL client. From Mysql to ADB, the historical data has been synchronized normally
猜你喜欢
柏拉图和他的三个弟子的故事:如何寻找幸福?如何寻找理想伴侣?
STM32中AHB总线_APB2总线_APB1总线这些是什么
Deep understanding of UDP, TCP
SolidWorks工程图中添加中心线和中心符号线的办法
【ORM框架】
The new activity of "the arrival of twelve constellations and goddesses" was launched
Postman interface test I
字节跳动 Kitex 在森马电商场景的落地实践
ORM -- query type, association query
Postman interface test IV
随机推荐
Word自动生成目录的方法
Mongodb creates an implicit database as an exercise
一文讲解单片机、ARM、MUC、DSP、FPGA、嵌入式错综复杂的关系
Future development blueprint of agriculture and animal husbandry -- vertical agriculture + artificial meat
The physical meaning of imaginary number J
内存==c语言1
The new activity of "the arrival of twelve constellations and goddesses" was launched
ORM--数据库增删改查操作逻辑
Postman interface test V
企业实战|复杂业务关系下的银行业运维指标体系建设
Pytest learning - dayone
Write it into the SR table in the way of flinksql. It is found that the data to be deleted has not been deleted. Refer to the document https://do
Some thoughts on the testing work in the process of R & D
LLVM之父Chris Lattner:为什么我们要重建AI基础设施软件
14th test
嵌入式背景知识-芯片
“十二星座女神降临”全新活动推出
ORM模型--关联字段,抽象模型类
web3.0系列之分布式存储IPFS
[learning notes - Li Hongyi] Gan (generation of confrontation network) full series (I)