当前位置:网站首页>大整数类实现阶乘
大整数类实现阶乘
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;
}边栏推荐
- Before joining the chain home, I made a competitive product analysis for myself
- Apprentissage avancé des fonctions en es6
- [ORM framework]
- Please ask me a question. I started a synchronization task with SQL client. From Mysql to ADB, the historical data has been synchronized normally
- 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...)
- Web3.0 series distributed storage IPFs
- 高斯消元
- Advanced function learning in ES6
- 【学习笔记-李宏毅】GAN(生成对抗网络)全系列(一)
- A wave of open source notebooks is coming
猜你喜欢

ISP、IAP、ICP、JTAG、SWD的编程特点

Qualifying 3
![[learning notes - Li Hongyi] Gan (generation of confrontation network) full series (I)](/img/94/b4df1ce2861a851fcd8de3e08540b0.png)
[learning notes - Li Hongyi] Gan (generation of confrontation network) full series (I)

Basic chapter: take you through notes

Introduction to energy Router: Architecture and functions for energy Internet

Flex flexible layout

XML配置文件解析与建模

AI moves from perception to intelligent cognition

Video based full link Intelligent Cloud? This article explains in detail what Alibaba cloud video cloud "intelligent media production" is

虚数j的物理意义
随机推荐
Es classes and objects, prototypes
运用tensorflow中的keras搭建卷积神经网络
【ORM框架】
ArcGIS operation: converting DWG data to SHP data
Natapp intranet penetration
STM32产品介绍
Finally, there is no need to change a line of code! Shardingsphere native driver comes out
Check the example of where the initialization is when C initializes the program
Pytest learning - dayone
SQLyog数据库怎么取消自动保存更改
Main (argc, *argv[]) details
单片机(MCU)最强科普(万字总结,值得收藏)
Bit operation ==c language 2
China's first electronic audio category "Yamano electronic audio" digital collection is on sale!
视频化全链路智能上云?一文详解什么是阿里云视频云「智能媒体生产」
第十四次试验
arcgis操作:dwg数据转为shp数据
Basic use of JMeter to proficiency (I) creation and testing of the first task thread from installation
Google colab loads Google drive (Google drive is used in Google colab)
Future development blueprint of agriculture and animal husbandry -- vertical agriculture + artificial meat