其他分享
首页 > 其他分享> > 调和级数

调和级数

作者:互联网

公式:f(n)=ln(n)+C+1/(2*n)

当n < 10000时,直接算,大于10000时用公式,其中C≈0.57721566490153286060651209

#include<bits/stdc++.h>
using namespace std;
const double r = 0.57721566490153286060651209;
double a[10000];

int main() {
    a[1] = 1;
    for (int i = 2; i < 10000; i++) {
        a[i] = a[i - 1] + 1.0 / i;
    }
    int n;
    cin >> n;
    for (int kase = 1; kase <= n; kase++) {
        int n;
        cin >> n;
        if (n < 10000) {
            printf("Case %d: %.10lf\n", kase, a[n]);
        } else {
            double a = log(n) + r + 1.0 / (2 * n);
            // double a=log(n+1)+r;
            printf("Case %d: %.10lf\n", kase, a);
        }
    }
    return 0;
}

  

标签:%.,10000,int,double,调和级数,printf,kase
来源: https://www.cnblogs.com/qq1415584788/p/14803895.html