首页 > 代码库 > 大数阶乘

大数阶乘

A - N!
Time Limit:5000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit Status

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
 数组模拟
我跑了一个10000的数据,显示超时根本算不出来,可提交竟然对了,10000!真是个天文数字。
#include<stdio.h>
#include<string.h>
int main(){
	int a[101000],n,l;
	while(~scanf("%d",&n)){
		memset(a,0,sizeof(a));
		a[0] =1;
		l=1;
		for(int i=1;i<=n;i++){
			int s = 0,j;
			for( j=0;j<l||s;j++){
				  int z= a[j]*i+s;
					a[j] = z%10;
					s = z/10;
			}
			l=j;
			//printf("%d\n",l);
		}
		for(int j = l-1;j>=0;j--)printf("%d",a[j]);
		printf("\n");
	}
}