Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 81565 Accepted Submission(s): 23924
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 <algorithm>
#include <string>
#include <cstring>
using namespace std;
int a[10000000];
int main() {
int n;
while(cin>>n) {
memset(a,0,sizeof(a));
a[0]=1;
int tmp;
int flag=0;//高位数
for(int i=2;i<=n;i++) {
int t=flag;
for(int j=0;j<=t;j++) {
tmp+=a[j]*i;
a[j]=tmp%10;
tmp=tmp/10;//保存下一位的进数
}
while(tmp) {
a[++flag]=tmp%10;
tmp=tmp/10;
}
}
int x;
for(int i=10000000;i>=0;i--) {
if(a[i]) {
x=i;
break;
}
}
for(int i=x;i>=0;i--) {
printf("%d",a[i]);
}
printf("\n");
}
return 0;
}