N!
Time Limit : 10000/5000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : Accepted Submission(s) :
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
大数的阶乘,用到数组,一组组储存数据,然后统一输出,用循环一个数一个数乘就不会导致空间不足,下面代码是没5位一进位的,代码不复杂,认真看懂吧!
代码如下:
#include<stdio.h>
int main()
{
int a[100001],b,n,m,i,j;
while(~scanf("%d",&n))
{
m=0;a[0]=1;
for(i=1;i<=n;i++)
{
b=0;
for(j=0;j<=m;j++)
{
a[j]=a[j]*i+b;
b=a[j]/100000;
a[j]=a[j]%100000;
}//数组存数
if(b>0)
{
m++;
a[m]=b;
}
}
printf("%d",a[m]);//将最高位的几位数分开输出,避开前置补0.
for(i=m-1;i>=0;i--)
printf("%05d",a[i]);//每5位一输出
printf("\n");
}
return 0;
}原文:http://blog.csdn.net/hanhai768/article/details/23622403