码迷,mamicode.com
首页 > 其他好文 > 详细

hdu_1018(斯大林公式/n!的位数)

时间:2017-04-05 09:42:09      阅读:358      评论:0      收藏:0      [点我收藏+]

标签:clu   img   class   open   code   ace   turn   splay   scanf   

题意:求大数n!的位数。

根据n! = (int)log(n!)+1

方法1:

log(n!) = log(1*2*3*...*n) = log1+log2+...+logn

方法2:

斯大林公式:

n! = sqrt(2*PI*n)*(n/e)^n

两侧取对数有

log10(n!) = 1/2log(2*PI*n) + n*log(n/e)

 

code1:

技术分享
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cmath>
 4 using namespace std;
 5 int main()
 6 {
 7     int n;
 8     scanf("%d",&n);
 9     int t;
10     while(n--)
11     {
12         scanf("%d",&t);
13         double ans = 0;
14         for(int i = 1; i <= t; i++){
15             ans+=log10(i);
16         }
17         ans++;
18         printf("%d\n",(int)ans);
19     }
20     return 0;
21 }
View Code

 

code2:

技术分享
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <math.h>
 4 #define e 2.71828182
 5 #define PI acos(-1)
 6 int main()
 7 {
 8     int t;
 9     int n;
10     double w;             //斯特林数
11     scanf("%d",&t);
12     while(t--)
13     {
14         scanf("%d",&n);
15         w=(1.0/2*log10(2*PI*n)+n*log10(n/e));
16         printf("%d\n",(int) w+1);              //记得+1,不能少。
17     }
18     return 0;
19 }
View Code

 

hdu_1018(斯大林公式/n!的位数)

标签:clu   img   class   open   code   ace   turn   splay   scanf   

原文地址:http://www.cnblogs.com/shanyr/p/6667170.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!