HDU 1042 N!
N!
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 83996 Accepted Submission(s): 24766
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
Author
JGShining(极光炫影)
Recommend
分析:一开始以为要用高精度,然后看了下 其他人的做法,原来可以直接用数组储存当前位
然后又因为不知道 0!=1 WA了很多发..
注意,一开始数组需要多长的话,可能需要试探性的取吧,再看ans[MAXN]是否为0
代码如下:
#include <cstdio> #include <iostream> #include <cstring> #include <map> #include <algorithm> using namespace std; typedef long long ll; const int MAXN=40000; int ans[MAXN]; int flag; int main() { int n,r,tmp; while(scanf("%d",&n)!=EOF){ flag=0; memset(ans,0,sizeof(ans)); ans[0]=1; for(int i=2;i<=n;i++) { r=0; for(int j=0;j<=MAXN;j++) { tmp=ans[j]*i+r; ans[j]=tmp%10; r=tmp/10; } } for(int i=MAXN;i>=0;i--) { if(flag==0&&ans[i]==0) continue; else { flag=1; printf("%d",ans[i]); } } printf("\n"); } return 0; }