ACM HDU 1042 N!(高精度计算阶乘)

时间:2022-10-03 03:38:37

N!

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 24579    Accepted Submission(s): 6774


Problem Description
Given an integer N(0 ≤ N ≤ 10000), your task is to calculate N!
 

Input
One N in one line, process to the end of file.
 

Output
For each N, output N! in one line.
 

Sample Input
123
 

Sample Output
126
 

Author
JGShining(极光炫影)
 
 
#include<stdio.h>
#include
<string.h>
const int MAXN=40000;//如果是10000的阶乘,改为40000就够了
int f[MAXN];
int main()
{
int i,j,n;
while(scanf("%d",&n)!=EOF)
{
memset(f,
0,sizeof(f));
f[
0]=1;
for(i=2;i<=n;i++)
{
int c=0;
for(j=0;j<MAXN;j++)
{
int s=f[j]*i+c;
f[j]
=s%10;
c
=s/10;
}
}
for(j=MAXN-1;j>=0;j--)
if(f[j]) break;//忽略前导0
for(i=j;i>=0;i--) printf("%d",f[i]);
printf(
"\n");
}
return 0;
}