题意:求大数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

相关文章:

  • 2022-02-06
  • 2022-12-23
  • 2021-12-26
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-12-14
  • 2021-10-30
  • 2022-12-23
  • 2021-08-02
  • 2022-12-23
  • 2021-05-10
相关资源
相似解决方案