当前位置:网站首页>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;
}边栏推荐
- 【剑指Offer】42. 栈的压入、弹出序列
- SolidWorks工程图中添加中心线和中心符号线的办法
- ES6中的函数进阶学习
- Mongodb creates an implicit database as an exercise
- Video based full link Intelligent Cloud? This article explains in detail what Alibaba cloud video cloud "intelligent media production" is
- Pit encountered by vs2015 under win7 (successful)
- ISP、IAP、ICP、JTAG、SWD的编程特点
- 搭建物联网硬件通信技术几种方案
- Wallys/IPQ6010 (IPQ6018 FAMILY) EMBEDDED BOARD WITH ON-BOARD WIFI DUAL BAND DUAL CONCURRENT
- Google colab loads Google drive (Google drive is used in Google colab)
猜你喜欢

ES类和对象、原型

ORM--分组查询,聚合查询,查询集QuerySet对象特性

Performance optimization record of the company's product "yunzhujia"

How to cancel automatic saving of changes in sqlyog database

Delete a record in the table in pl/sql by mistake, and the recovery method

Pdf document signature Guide

Programming features of ISP, IAP, ICP, JTAG and SWD

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

The request object parses the request body and request header parameters

高数_第1章空间解析几何与向量代数_向量的数量积
随机推荐
学习记录——高精度加法和乘法
STM32产品介绍
The combination of over clause and aggregate function in SQL Server
Pytest learning - dayone
中国首款电音音频类“山野电音”数藏发售来了!
串口通讯继电器-modbus通信上位机调试软件工具项目开发案例
uboot机构简介
ORM model -- creation and query of data records
每周推荐短视频:L2级有哪些我们日常中经常会用到的功能?
LeetCode 练习——113. 路径总和 II
Finally, there is no need to change a line of code! Shardingsphere native driver comes out
PDF文档签名指南
Some thoughts on the testing work in the process of R & D
柏拉图和他的三个弟子的故事:如何寻找幸福?如何寻找理想伴侣?
ORM--逻辑关系与&或;排序操作,更新记录操作,删除记录操作
喜马拉雅网页版每次暂停后弹窗推荐下载客户端解决办法
[ORM framework]
Some test points about coupon test
反射效率为什么低?
Appx code signing Guide