nyoj 28 nyoj 69-大数阶乘

http://acm.nyist.net/JudgeOnline/problem.php?pid=28 大数水题~~

 1 #include<stdio.h>
2 int a[20000];
3 void ch(int *a, int n)
4 {
5 int i, k;
6 for(i = 19999; i >= 0; i--)
7 {
8 if(a[i])
9 break;
10 }
11 k = i;
12 for(i = 0; i <= k; i++)
13 {
14 a[i] = a[i] * n;
15 }
16 for(i = 0; i < 20000; i++)
17 {
18 if(a[i] > 9)
19 {
20 a[i+1] += a[i] / 10;
21 a[i] = a[i] % 10;
22 }
23 }
24 }
25 int main()
26 {
27 int n, i;
28 a[0] = 1;
29 scanf("%d", &n);
30 for(i = 1; i <= n; i++)
31 ch(a, i);
32 for(i = 19999; i >= 0; i--)
33 {
34 if(a[i])
35 break;
36 }
37 for(; i >= 0; i--)
38 printf("%d", a[i]);
39 putchar(10);
40 return 0;
41 }

 这是具体计算阶乘的~

如果只需要计算大数阶乘的位数,像http://acm.nyist.net/JudgeOnline/problem.php?pid=69,有公式可以套~~~ n!的位数 = log10(2*PI*n)/2+n*log10(n/e);

代码如下:

 1 #include<iostream>
2 #include<math.h>
3 using namespace std;
4 int main()
5 {
6 int tcases, n;
7 cin >> tcases;
8 while(tcases--)
9 {
10 cin >> n;
11 if(n == 1)
12 cout << 1 << endl;
13 else
14 {
15 int result = (int) (log10(4.0 * acos(0.0) * n)/2 + 1.0 * n * log10(1.0 *n / exp(1.0))) + 1; //arccos(0)= PI/2
16 cout << result << endl;
17 }
18 }
19 return 0;
20 }

 

 

你可能感兴趣的:(阶乘)