当前位置:网站首页>大数阶乘计算
大数阶乘计算
2022-07-29 16:00:00 【封子墨】
阶乘计算
资源限制
时间限制: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
大致思路就是,先直接计算,然后把结果存放到数组中,不妨先设结果为A,当A大于9时,需要进位,这时把该进位的值放到数组的后一位中,然后把A的个位保存再原位置即可,在这儿我使用了vector容器来存放数据。
#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");
}
}
边栏推荐
- 一文读懂Elephant Swap,为何为ePLATO带来如此高的溢价?
- 718. 最长重复子数组
- Practice of Weibo Advertising Operation and Maintenance Technology Supporting Ten Billions of Requests
- 国内EDA领导者芯和半导体完成最新一轮超亿元融资
- MLX90640 红外热成像仪开发笔记(九)
- This article penetrates the architecture design and cluster construction of the distributed storage system Ceph (hands-on)
- 错排问题详解
- 蚂蚁三面滑铁卢!遭分布式截胡,靠这些笔记潜修 30 天,挺进京东
- 面试官:小伙子你来说说MySQL底层架构设计
- 双非渣渣的上岸之路!备战 60 天,三战滴滴侥幸收获 Offer
猜你喜欢
718. The longest repeat subarray
3. SAP ABAP OData 服务诊断工具 /IWFND/ERROR_LOG 的使用方法
Ribbon自定义修改负载均衡
ByteArrayOutputStream class source code analysis
[Designers must learn] Lighting skills of Enscape in SketchUp
中小型金融企业该如何进行灾备建设?
[Server Storage Data Recovery] A data recovery case of a RAID 5 crash caused by the failure of a certain model of Huawei OceanStor storage RAID 5 hard disk and the failure to synchronize data with the
Turbine聚合监控
【C语言刷题】Leetcode268丢失的数字
设置工作模式与环境
随机推荐
How should small and medium-sized financial enterprises carry out disaster recovery construction?
一文参透分布式存储系统Ceph的架构设计、集群搭建(手把手)
pjax无法生效解决办法,butterfly主题维护你的pjax
美国对集成电路及其产品发起337调查:英特尔/联想/宏碁/华硕/微星均涉及其中
Turbine Aggregation Monitoring
中小型金融企业该如何进行灾备建设?
最新!多交的税可以退,同学,你今天退税了吗?
如何破坏单例?我说了好几种方式,面试官:没想到你真会
使用DataEase开源工具制作一个高质量的数据大屏
AI全流程开发难题破解之钥
如何在C语言中定义自己的数据类型?
Property (Property Animation Animation), the basic use of Butterknife butter knife
中国厂商统治印度智能手机市场,份额提升至77%
TensorFlow Serving 高性能的机器学习模型服务系统
【小程序项目开发-- 京东商城】uni-app之商品列表页面 (上)
nacos实现基本的服务跨进程调用和使用OpenFeign进行服务跨进程调用
linux 安装mysql8.0 超详细教程(实战多次)
GBJ2510-ASEMI电机专用25A整流桥GBJ2510
pycaret在钻石数据集上的使用 - 回归问题
tcp的四次挥手(为什么三次握手和四次挥手)