N!
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 34337 Accepted Submission(s): 9604
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
AC代码:
#include<iostream>
using namespace std;
int a[40000];
int main()
{
int N,i,j,pre;
while(cin>>N)
{
a[0]=1;
a[1]=1;
if(N==0||N==1){cout<<1<<endl;continue;}
for(i=2;i<=N;i++)
{
pre=0;
for(j=1;j<=a[0];j++)
{
a[j]=a[j]*i;
a[j]+=pre;//加上进位
pre=a[j]/10;//进位值
a[j]%=10;//本位
}
while(pre)
{
a[j]=pre%10;
a[0]=j;
pre/=10;
j++;
}
}
for(i=a[0];i>=1;i--)
{cout<<a[i];}
cout<<endl;
}
return 0;
}