当前位置:网站首页>大整数类实现阶乘
大整数类实现阶乘
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;
}边栏推荐
- Become a "founder" and make reading a habit
- ORM -- database addition, deletion, modification and query operation logic
- 单片机(MCU)最强科普(万字总结,值得收藏)
- CDZSC_2022寒假个人训练赛21级(1)
- ORM--数据库增删改查操作逻辑
- Hcip first day notes sorting
- ORM--逻辑关系与&或;排序操作,更新记录操作,删除记录操作
- Application of C # XML
- Sword finger offer II 107 Distance in matrix
- Agile course training
猜你喜欢

STM32中AHB总线_APB2总线_APB1总线这些是什么
![[ORM framework]](/img/72/13eef38fc14d85978f828584e689a0.png)
[ORM framework]

CentOS installs JDK1.8 and mysql5 and 8 (the same command 58 in the second installation mode is common, opening access rights and changing passwords)

一文讲解单片机、ARM、MUC、DSP、FPGA、嵌入式错综复杂的关系

ORM--查询类型,关联查询

每周推荐短视频:L2级有哪些我们日常中经常会用到的功能?

Internship log - day07

Win10 installation vs2015
![[original] what is the core of programmer team management?](/img/11/d4b9929e8aadcaee019f656cb3b9fb.png)
[original] what is the core of programmer team management?

一大波开源小抄来袭
随机推荐
Flinkcdc failed to collect Oracle in the snapshot stage. How do you adjust this?
官媒关注!国内数字藏品平台百强榜发布,行业加速合规健康发展
Applet sliding, clicking and switching simple UI
Agile course training
嵌入式背景知识-芯片
The request object parses the request body and request header parameters
Writing file types generated by C language
ORM -- logical relation and & or; Sort operation, update record operation, delete record operation
ORM模型--关联字段,抽象模型类
Pit using BigDecimal
ORM model -- creation and query of data records
SolidWorks工程图中添加中心线和中心符号线的办法
Video based full link Intelligent Cloud? This article explains in detail what Alibaba cloud video cloud "intelligent media production" is
Analyze Android event distribution mechanism according to popular interview questions (II) -- event conflict analysis and handling
ORM模型--数据记录的创建操作,查询操作
Bit operation ==c language 2
Huffman encoded compressed file
【ORM框架】
使用BigDecimal的坑
企业实战|复杂业务关系下的银行业运维指标体系建设