当前位置:网站首页>Factorial implementation of large integer classes
Factorial implementation of large integer classes
2022-07-07 10:16:00 【Last night was peaceful and Chang'an_】
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int maxn = 10000;
struct BigInt
{
BigInt(int x = 0)
{
memset(arr, 0, sizeof(arr));// Initialize all to 0
for (len = 1; x; len++)
{
arr[len] = x % 10;// Yes x Reverse order preservation
x /= 10;
}
len--;
}
int& operator[](int i)
{
return arr[i];// use x[i] Instead of x.arr[i]
}
void flatten(int L)// Handle bits that are not single digits into single digits , So it is called flattening ,L Not less than the effective length
{
len = L;
for (int i = 1; i <= len; ++i)
{
arr[i + 1] += arr[i] / 10;
arr[i] %= 10;
}
while (!arr[len])// Eliminate pre 0
{
len--;
}
}
void print()// Reverse output
{
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);// The maximum length shall not exceed 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] The result of is produced in i+j-1 On a
}
}
c.flatten(len );// The maximum length shall not exceed 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 Type longest 10 position , So you can flatten it like this
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;
}边栏推荐
- UnityWebRequest基础使用之下载文本、图片、AB包
- LLVM之父Chris Lattner:为什么我们要重建AI基础设施软件
- STM32 Basics - memory mapping
- 单片机(MCU)最强科普(万字总结,值得收藏)
- Enterprise practice | construction of banking operation and maintenance index system under complex business relations
- Download Text, pictures and ab packages used by unitywebrequest Foundation
- SolidWorks工程图中添加中心线和中心符号线的办法
- Appx代碼簽名指南
- 学习记录——高精度加法和乘法
- Leetcode exercise - 113 Path sum II
猜你喜欢

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

ORM model -- associated fields, abstract model classes

ORM -- database addition, deletion, modification and query operation logic

ORM模型--关联字段,抽象模型类

Agile course training

【二开】【JeecgBoot】修改分页参数

Enterprise practice | construction of banking operation and maintenance index system under complex business relations

Methods of adding centerlines and centerlines in SolidWorks drawings

Delete a record in the table in pl/sql by mistake, and the recovery method
![[untitled]](/img/5b/61efbaded29250bc8d921b0cf087c8.png)
[untitled]
随机推荐
How to cancel automatic saving of changes in sqlyog database
【学习笔记-李宏毅】GAN(生成对抗网络)全系列(一)
ORM -- database addition, deletion, modification and query operation logic
ORM模型--关联字段,抽象模型类
ORM模型--数据记录的创建操作,查询操作
web3.0系列之分布式存储IPFS
Why are social portals rarely provided in real estate o2o applications?
Can I open a stock trading account online? Is it safe
ArcGIS operation: converting DWG data to SHP data
MongoDB创建一个隐式数据库用作练习
能源路由器入门必读:面向能源互联网的架构和功能
ORM--分组查询,聚合查询,查询集QuerySet对象特性
EasyExcel读取写入简单使用
Win10安装VS2015
In addition to the objective reasons for overtime, what else is worth thinking about?
运用tensorflow中的keras搭建卷积神经网络
【ORM框架】
Introduction to automated testing framework
Or in SQL, what scenarios will lead to full table scanning
AI moves from perception to intelligent cognition