有關大數的相關用法,請參考
http://celinechiu0809.blogspot.tw/2015/04/c-big-numnbers.html
題目連結
題目大意
輸出階層,最大為1000!
解題思路
標準的大數題。這一題因為測資數*位數太過龐大,從頭算的話非常容易TLE,所以先開陣列存1!~1000!,後查表即可。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
| #include <cstdio> #include <cstdio> #include <cstdlib> #include <cmath> #define MAX_LENGTH 3000 #define DIGIT 10 int ans[1001][3000]; using namespace std; void big_mul(int *num1, int num2, int *answer){ int carry=0; for (int i=0; i< MAX_LENGTH; i++) answer[i]=0; for (int i=0; i< MAX_LENGTH; i++){ int t = answer[i] + num1[i]*num2 + carry; answer[i] = t%10; carry = t/10; } } void big_fact(){ for (int i=0; i< 1001; i++) { for (int j=0; j< 3000; j++) { ans[i][j]=0; } } ans[0][0] = 1; ans[1][0] = 1; for (int i=2; i<=1000; i++) big_mul(ans[i-1], i, ans[i]); } int main() { int n; big_fact(); while (scanf( "%d" , &n)!=EOF){ int flag=0; printf( "%d!\n" ,n); for (int i=MAX_LENGTH-1; i>=0; i--){ if (flag==0 && ans[n][i]!=0) flag=1; if (flag==1) printf( "%d" , ans[n][i]); } printf( "\n" ); } return 0; } |