当前位置:网站首页>large number factorial calculation
large number factorial calculation
2022-07-29 17:14:00 【Seal the son ink】
阶乘计算
资源限制
时间限制:1.0s 内存限制:512.0MB
问题描述
输入一个正整数n,输出n!的值.
其中n!=1*2*3*…*n.
算法描述
n!可能很大,而计算机能表示的整数范围有限,需要使用高精度计算的方法.使用一个数组A来表示一个大整数a,A[0]表示a的个位,A[1]表示a的十位,依次类推.
将a乘以一个整数k变为将数组A的每一个元素都乘以k,请注意处理相应的进位.
首先将a设为1,然后乘2,乘3,当乘到n时,即得到了n!的值.
输入格式
输入包含一个正整数n,n<=1000.
输出格式
输出n!的准确值.
样例输入
10
样例输出
3628800
大致思路就是,先直接计算,Then store the result in an array,Might as well set the result as A,当A大于9时,需要进位,At this time, the value of the carry is placed in the last bit of the array,然后把AThe units digit can be saved to the original location,Here I use itvectorcontainer to store data.
#include<cstdio>
#include<vector>
using namespace std;
vector<int>a(0);
int main() {
int n;
scanf("%d",&n);
if(n==0)
printf("0\n");
else {
int m=1;
a.push_back(m);
while(m<=n) {
for(int i=0; i<a.size(); i++) {
a[i]=a[i]*m;
}
int jin=0;
for(int i=0; i<a.size(); i++) {
if(a[i]>9) {
jin=a[i];
a[i]=jin%10;
jin/=10;
if((i+1)<a.size()) {
a[i+1]+=jin;
} else
a.push_back(jin);
} else
continue;
}
m++;
}
for(int i=a.size()-1; i>=0; i--) {
printf("%d",a[i]);
}
printf("\n");
}
}
边栏推荐
- This article penetrates the architecture design and cluster construction of the distributed storage system Ceph (hands-on)
- MLX90640 infrared thermal imager development notes (9)
- 718. The longest repeat subarray
- Texture 】 【 terrain 】 【 virtual virtual terrain texture technology is introduced
- 面试突击69:TCP 可靠吗?为什么?
- 再见Postman!一款更适合国人的接口管理工具
- Knowledge Graph Construction of Mall Commodities
- Loadrunner与Jmeter区别与相同
- 节省70%的显存,训练速度提高2倍!浙大&阿里提出在线卷积重新参数化OREPA,代码已开源!(CVPR 2022 )...
- Google Play 政策更新 | 2022 年 7 月
猜你喜欢
ByteArrayOutputStream 类源码分析
RocketQA:通过跨批次负采样(cross-batch negatives)、去噪的强负例采样(denoised hard negative sampling)与数据增强(data augment
Flutter动态化 | Fair 2.6.0 新版本特性
百面量化金融
MUD DAO火爆入世,DAO主轮募集蓄势待发
浅谈程序的内存布局
Greedy (1) interval complete coverage problem
Moving forward steadily without forgetting the original intention, Volvo's sense of security comes from the public's recognition
PL5902 SOT-23-5 高效1MHz2A同步DC-DC降压调节器 百盛电子代理商
Ernie-gram, 显式、完备的 n-gram 掩码语言模型,实现了显式的 n-gram 语义单元知识建模。
随机推荐
中小型金融企业该如何进行灾备建设?
Moving forward steadily without forgetting the original intention, Volvo's sense of security comes from the public's recognition
GMAT考什么?
【上传文件】
旭硝子龟尾工厂3月起将减少30%玻璃基板供应!TCL华星、友达、群创、惠科均受影响
鸡兔同笼
LinkedList 5-141. The circular linked list
[网络知识]路由OSPF
动态规划 爬楼梯
mysql的sql分页查询语句怎么写_sql 分页查询语句(mysql分页语句)「建议收藏」
Win10 check sha256
美国对集成电路及其产品发起337调查:英特尔/联想/宏碁/华硕/微星均涉及其中
Kubernetes 的 5 个误区
溢价20%,瑞萨电子宣布拟以60亿美元收购Dialog
稳步向前不忘初心,沃尔沃的安全感来自公众的认可
wooden stick
[网络]LAN技术堆叠与集群
This article penetrates the architecture design and cluster construction of the distributed storage system Ceph (hands-on)
Database Project 01 Documentation: Database Skills Needed for Software Testing
HMS Core音频编辑服务音源分离与空间音频渲染,助力快速进入3D音频的世界