zoukankan      html  css  js  c++  java
  • 求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;
        }
    }
  • 相关阅读:
    递归 迷宫问题
    中缀表达式转后缀表达式
    栈实现后缀表达式计算

    单向环形链表
    站在巨人的肩上
    C#自宿主API,不依赖IIS
    MySQL 安装失败解决办法
    第一话
    村上春树《眠》读书笔记
  • 原文地址:https://www.cnblogs.com/bestwzh/p/6637573.html
Copyright © 2011-2022 走看看