1. 程式人生 > >HDU 1042 N!

HDU 1042 N!

careful gree pac i++ shining you weight tle accep

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 We have carefully selected several similar problems for you: 1715 1047 1063 1753 1316 分析:一開始以為要用高精度,然後看了下 其他人的做法,原來可以直接用數組儲存當前位 然後又因為不知道 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; }

HDU 1042 N!