求N!的长度【数学】 51nod 1058 1130

 

n!的长度等于log10(n!)

1
2
3
4
5
6
7
8
9
10
11
#include <bits/stdc++.h>
using namespace std;
int main() {
    int n;
    cin >> n;
    double ans = 1;
    for(int i = 1; i <= n; i++) {
        ans += log10(i);
    }
    cout << (int)ans << endl;
}

 

 

 

用斯特林公式求n!,然后log10(n!)即可

(如果怕n不够大下式不成立,可以当数小于10000时用for求阶层。不过51nod直接过了)

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <bits/stdc++.h>
#define PI 3.1415926535898
#define e 2.718281828459
using namespace std;
int main() {
    int T, n;
    cin >> T;
    while(T--) {
        cin >> n;
        double ans = log10(sqrt(2.0*PI*n)) + n*log10(n*1.0/e);// pow(n*1.0/e, n);
        cout << (long long)ans + 1 << endl;
    }
}
posted @ 2017-03-28 23:15  bestwzh  阅读(179)  评论(0编辑  收藏  举报