// N!.cpp : 定义控制台应用程序的入口点。
//
/*
N!
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 27897 Accepted Submission(s): 7646
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 "stdafx.h"
//结果缓存长度
#define MAX_RES_LEN 10000
int _tmain(int argc, _TCHAR* argv[])
{
int n; //输入的阶层数
int s; //单个结果与n相乘后的临时变量;
char res[MAX_RES_LEN]; //结果
int i;
while(scanf("%d",&n)!=EOF)
{
int carry = 0; //进位;
int residue = 0; //余数
res[MAX_RES_LEN - 1] = 1;
int lenRes = 1; //结果的长度
while (n-- >= 2)
{
for (i = 1; i <= lenRes; i++)
{
s = res[MAX_RES_LEN - i] * n + carry;
carry = s / 10;
residue = s % 10;
res[MAX_RES_LEN - i]= (char) residue;
}
while (carry != 0)
{
residue = carry % 10;
carry = carry / 10;
i++;
if (i > MAX_RES_LEN)
{
printf ("error!\n");
exit(0);
}
res[MAX_RES_LEN - i]= (char) residue;
}
lenRes = i - 1;
}
for (i = lenRes; i >= 1; i--)
{
printf ("%c", res[MAX_RES_LEN - i] + '0');
}
printf ("\n");
}
return 0;
}