N!

http://acm.hdu.edu.cn/showproblem.php?pid=1042

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


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>
using namespace std;
int main()
{
	int result[40000];
	int num;
	while(cin>>num)
	{
		int height=1;  //结果的最高位
		if(num>10000||num<0) break;
		result[0]=1;
		for(int i=1;i<=num;i++)
		{
			int res=0;  //进位
			for(int j=0;j<height;j++)
			{
				int buf=result[j]*i+res;  //计算结果
				result[j]=buf;   //取当前位
				res=buf/10;   //计算进位
			}
			while(res)
			{
				result[height++]=res;  //取当前位
				res/=10;  //计算进位
			}
		}
		for(int k=height-1;k>=0;k--)
			cout<<result[k];
		cout<<endl;
	}
	return 0;
}
posted on 2011-05-28 20:33  pcoda  阅读(914)  评论(0编辑  收藏  举报