Harmonic Number(调和级数+欧拉常数)

In mathematics, the nth harmonic number is the sum of the reciprocals of the first n natural numbers:

In this problem, you are given n, you have to find Hn.

Input
Input starts with an integer T (≤ 10000), denoting the number of test cases.

Each case starts with a line containing an integer n (1 ≤ n ≤ 108).

Output
For each case, print the case number and the nth harmonic number. Errors less than 10-8 will be ignored.

Sample Input
12

1

2

3

4

5

6

7

8

9

90000000

99999999

100000000

Sample Output
Case 1: 1

Case 2: 1.5

Case 3: 1.8333333333

Case 4: 2.0833333333

Case 5: 2.2833333333

Case 6: 2.450

Case 7: 2.5928571429

Case 8: 2.7178571429

Case 9: 2.8289682540

Case 10: 18.8925358988

Case 11: 18.9978964039

Case 12: 18.9978964139

题意:求f(n)=1/1+1/2+1/3+1/4…1/n   (1 ≤ n ≤ 108).,精确到10^-8。

题解:当n很小时,可直接求出结果,当n很大时,利用公式f(n)=ln(n)+C+1/(2*n),在C++ math库中,log即为ln;

代码:

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
const double r=0.57721566490153286060651209;//欧拉常数
double a[10000];
int main()
{
    a[1]=1;
    for (int i=2;i<10000;i++){//预先把小于10000的f(n)求出来
        a[i]=a[i-1]+1.0/i;
    }
    int n; cin>>n;
    for (int kase=1;kase<=n;kase++)
    {
        cin>>n;
        if (n<10000){//n<10000时,可直接得出结果
            printf("Case %d: %.10lf\n",kase,a[n]);
        }
        else{//否则利用欧拉公式
            double a=log(n)+r+1.0/(2*n);
            printf("Case %d: %.10lf\n",kase,a);
        }
    }
    return 0;
}

原文地址:https://www.cnblogs.com/LJHAHA/p/10453043.html

时间: 2024-08-04 14:25:34

Harmonic Number(调和级数+欧拉常数)的相关文章

Light OJ 1234 Harmonic Number 调和级数部分和

题目来源:Light OJ 1234  Harmonic Number 题意: 思路:没思路啊 这个是高数的东西 发散 n足够大时它无穷大 直接公式解 #include <cstdio> #include <cstring> #include <cmath> #include <string> #include <algorithm> #include <iostream> using namespace std; const int

LightOJ 1234 Harmonic Number(打表 + 技巧)

http://lightoj.com/volume_showproblem.php?problem=1234 Harmonic Number Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1234 Description In mathematics, the nth harmonic number is the sum of th

1245 - Harmonic Number (II)(规律题)

1245 - Harmonic Number (II)   PDF (English) Statistics Forum Time Limit: 3 second(s) Memory Limit: 32 MB I was trying to solve problem '1234 - Harmonic Number', I wrote the following code long long H( int n ) {     long long res = 0;     for( int i =

[2016-04-21][light]OJ[1234][Harmonic Number]

时间:2016-04-21 22:18:26 星期四 题目编号:[2016-04-21][light]OJ[1234][Harmonic Number] 题目大意:求∑nk=11kn∈(1,108),精确到10?8求∑k=1n1kn∈(1,108),精确到10?8 分析: 想法是打表,然后输出,但是直接打表会爆内存 解决办法,就是每隔100个来打表,节省1100的空间,然后从那个值开始计算到当前值解决办法,就是每隔100个来打表,节省1100的空间,然后从那个值开始计算到当前值 对应的整百就是n

LightOJ1234 Harmonic Number

1 /* 2 LightOJ1234 Harmonic Number 3 http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1234 4 打表 分块 5 由于只有加法运算,1e8时间是可以承受的. 6 然而空间无法承受,于是以50个单位为一块进行分块. 7 */ 8 #include <cstdio> 9 #include <algorithm> 10 #include <cstring&

LightOJ 1234 Harmonic Number (打表)

Harmonic Number Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1234 Description In mathematics, the nth harmonic number is the sum of the reciprocals of the first n natural numbers: In this p

Harmonic Number LightOJ - 1234 (分段打表)

题意: 求调和级数,但n很大啦.. 解析: 分段打表  每间隔50存储一个数,在计算时  只需要找到离输入的n最近的那个数 以它为起点 开始计算即可 emm...补充一下调和级数的运算公式   r为常数,r=0.57721566490153286060651209(r就是欧拉常数). 看一下这位的博客:https://www.cnblogs.com/weiyuan/p/5737273.html #include <iostream> #include <cstdio> #inclu

lightoj1234调和级数+欧拉常数

数据比较小时直接算,数据比较大时用公式1+1/2+...+1/n=inn+C+1/(2*n)   ,C是欧拉常数0.5772156649 刚开始用的这个1+1/2+...+1/n=in(n+1)+C,结果一直wa.... #include<map> #include<set> #include<cmath> #include<queue> #include<stack> #include<vector> #include<cst

LightOJ - 1245 Harmonic Number (II) 求同值区间的和

题目大意:对下列代码进行优化 long long H( int n ) {    long long res = 0;    for( int i = 1; i <= n; i++ )        res = res + n / i;    return res;} 题目思路:为了避免超时,要想办法进行优化 以9为例: 9/1 = 9 9/2 = 4 9/3 = 3 9/4 = 2 9/5 = 1 9/6 = 1 9/7 = 1 9/8 = 1 9/9 = 1 拿1来看,同为1的区间长度为:9