问题陈述:
杭州电子科技大学 HANGZHOU DIANZI UNIVERSITY Online Judge Problem - 1018
问题解析:
公式一:
n! = 10^m => lg(10^m) = lg(n!) => m = lg(n) + lg(n-1) + lg(n-2) + ... + lg1;
所以digits = (int)m + 1;
公式二:stirling公式
n! ≈ √2PIn(n/e)n
化简:lg(n!) = 1/2lg(2*PI*n) + nlg(n/e);
代码详解:
I:
1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 5 using namespace std; 6 7 int main() 8 { 9 int i, n, t, digits; 10 double m; 11 cin >> n; 12 while(n--) { 13 m = 0; 14 cin >> t; 15 for(i=1; i<=t; i++) { 16 m += log10(i*1.0); 17 } 18 digits = (int)m + 1; 19 cout << digits << endl; 20 } 21 return 0; 22 }
II:
1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 5 using namespace std; 6 7 int main() 8 { 9 int n, t, digits; 10 double PI = acos(double(-1)); 11 double e = exp(double(1)); 12 cin >> n; 13 while(n--) { 14 cin >> t; 15 digits = (int)(0.5*log10(2*PI*t) + t*log10(t/e)) + 1; 16 cout << digits << endl; 17 } 18 return 0; 19 }
转载请注明出处:http://www.cnblogs.com/michaelwong/p/4287232.html
时间: 2024-10-04 03:34:43