HDUOJ 1042 N!
N!
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 56209 Accepted Submission(s): 15948
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> #include <cstdio> #include <cstring> #define N 50000 using namespace std; int fac[N]; int calc(int n) { memset(fac,0,sizeof(fac)); fac[0] = 1; for(int i=1; i<=n; i++) { for(int j=0; j<N; j++) fac[j] *= i; for(int j=0; j<N-1; j++) { if(fac[j]>=10) { fac[j+1] += fac[j]/10; fac[j] = fac[j]%10; } } } for(int i=N-1; i>=0;i--) { if(fac[i]!=0) return i; } } int main() { int n; while(scanf("%d",&n)!=EOF) { int i; i = calc(n); while(i>=0) { printf("%d",fac[i]); i--; } printf("\n"); } return 0; }