N!
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 66145 Accepted Submission(s): 18997
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
1 2 3
Sample Output
1 2 6
//这个阶乘问题要特别注意判断需要多大的数组存储,我调试了好几次才好的
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int a[8001],n;
int main()
{
while(scanf("%d",&n)!=EOF)
{
int i,j;
memset(a,0,sizeof(a));
for(i=2,a[0]=1;i<=n;i++)//乘以i
{
for(j=0;j<8000;j++) a[j]*=i;
for(j=0;j<8000;j++)
{
a[j+1]+=a[j]/100000;
a[j]%=100000;
}
}
for(i=8000;i>=0&&!a[i];i--);//忽略前导0
printf("%d",a[i--]);
for(;i>=0;i--) printf("%05d",a[i]);
printf("\n");
}
return 0;
}