hdu1018 Big Number---N!的位数

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=1018

题目大意:

求n阶乘的位数
思路:

N的阶乖的位数等于LOG10(N!)=LOG10(1)+.....LOG10(N)

这里的解应该对上述结果向上取整

一开始直接输出cout<<ceil(ans)<<endl;这样出错,是因为ceil的返回值是double类型的,这里应该强制转化成(int)后输出就不会错了

还可以用斯特林公式

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<set>
 6 #include<cmath>
 7 using namespace std;
 8 const int maxn = 1e7 + 10;
 9 typedef long long ll;
10 ll T, n, m;
11 int main()
12 {
13     //for(int i = 2; i <= 10000000; i++)a[i] = a[i - 1] + log10(i);这样预处理是超内存的
14     //a[1] = 1;
15     cin >> T;
16     while(T--)
17     {
18         cin >> n;
19         if(n == 1)cout<<"1"<<endl;
20         else
21         {
22             double tot = 0;
23             for(int i = 2; i <= n; i++)
24                 tot += log10(1.0 * i);
25             int ans = ceil(tot);
26             cout<<ans<<endl;
27         }
28     }
29     return 0;
30 }
原文地址:https://www.cnblogs.com/fzl194/p/8690984.html