N! HDU - 1042
https://cn.vjudge.net/problem/HDU-1042
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 <bits/stdc++.h>
#define maxn 200000
using namespace std;
int f[maxn],len;
int main(){
int n;
while(~scanf("%d", &n)){
memset(f,0,maxn);
f[0]=1;
int ans=1;
for(int i=2;i<=n;i++){
int c=0;
for(int j=0;j<ans;j++){
int medium=f[j]*i+c;
f[j]=medium%10;
c=medium/10;
}
while(c){
f[ans]=c%10;
c=c/10;
ans++;
}
}
len=maxn;
while(len--)
if(f[len]) break;
len++;
while(len--) printf("%d", f[len]);
printf("\n");
}
return 0;
}