输入N求N的阶乘的准确值。
Input
输入N(1 <= N <= 10000)
Output
输出N的阶乘
Input示例
5
Output示例
120
题解:高精度加压位模板题,最高压位14位,140ms。
AC代码:
#include<cstdio>
using namespace std;
const long long N = 100000000000000;
const int now = 14;
int n;
long long a[10007];
int num(long long x)
{
int k = 0;
while(x>0)
{
x/=10;
k++;
}
return k;
}
int main()
{
scanf("%d",&n);
long long temp;
int cnt = 0;
a[0]=1;
for(int i=2;i<=n;i++)
{
temp = 0;
for(int j=0;j<=cnt;j++)
{
a[j]=a[j]*i+temp;
temp = a[j]/N;
a[j]=a[j]%N;
}
if(temp)
{
a[++cnt]=temp;
}
}
printf("%lld",a[cnt]);
for(int i = cnt-1;i>=0;i--)
{
int l = num(a[i]);
for(int j=0;j<now-l;j++)printf("0");
if(a[i])printf("%lld",a[i]);
}
printf("\n");
return 0;
}

本篇博客介绍了一道计算机科学领域的基础题目——计算N的阶乘。该问题通过使用高精度加压位的方法解决,实现了对于较大数值N(1 <= N <= 10000)阶乘的精确计算。
867

被折叠的 条评论
为什么被折叠?



