当前位置:网站首页>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");
}
}
边栏推荐
猜你喜欢

(notes) Build the was configured to -- Settings repositories over project repositories but solutions

RocketQA:通过跨批次负采样(cross-batch negatives)、去噪的强负例采样(denoised hard negative sampling)与数据增强(data augment

一文参透分布式存储系统Ceph的架构设计、集群搭建(手把手)

干货!如何使用仪表构造SRv6-TE性能测试环境

Practice of Weibo Advertising Operation and Maintenance Technology Supporting Ten Billions of Requests

【PCL学习笔记】点云处理常用的库和API(PCL库+Eigen)

RocketQA: across batches negative sampling (cross - batch negatives), the denoising of strong negative cases of sampling (denoised hard negative from) and data to enhance (data augment

Ernie-gram, 显式、完备的 n-gram 掩码语言模型,实现了显式的 n-gram 语义单元知识建模。

CRM如何帮助企业营销获客

Easy Genes: Human tRNA loci exhibit DNA hypermethylation associated with aging | Research Article
随机推荐
HMS Core音频编辑服务音源分离与空间音频渲染,助力快速进入3D音频的世界
factorial factorization
如何在C语言中定义自己的数据类型?
Database Project 01 Documentation: Database Skills Needed for Software Testing
Knowledge map construction whole process
Tess4J 图片文字识别
环球晶圆成功以43.5亿欧元获得Siltronic控股权
[极客大挑战 2019]LoveSQL 1
ASCII码排序
flutter如何实现切换页面缓存
Easy Genes: Human tRNA loci exhibit DNA hypermethylation associated with aging | Research Article
GBJ2510-ASEMI电机专用25A整流桥GBJ2510
Quantitative primary - akshare get the stock code, the minimalist strategy
Flutter动态化 | Fair 2.6.0 新版本特性
【地形】【虚拟纹理】地形虚拟纹理技术介绍
零花钱
CRM如何帮助企业营销获客
参与造谣传谣,华为宣布开除五名员工
Dynamic planning to climb the stairs
中国厂商统治印度智能手机市场,份额提升至77%