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[];
int main()
{
a[]=;
for (int i=;i<;i++){//预先把小于10000的f(n)求出来
a[i]=a[i-]+1.0/i;
}
int n; cin>>n;
for (int kase=;kase<=n;kase++)
{
cin>>n;
if (n<){//n<10000时,可直接得出结果
printf("Case %d: %.10lf\n",kase,a[n]);
}
else{//否则利用欧拉公式
double a=log(n)+r+1.0/(*n);
printf("Case %d: %.10lf\n",kase,a);
}
}
return ;
}

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

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

    题意:求f(n)=1/1+1/2+1/3+1/4-1/n   (1 ≤ n ≤ 108).,精确到10-8    (原题在文末) 知识点:      调和级数(即f(n))至今没有一个完全正确的公式, ...

  2. C - Harmonic Number(调和级数+欧拉常数)

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

  3. LightOJ 1234 Harmonic Number 调和级数部分和

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1234 Sample Input Sample Output Case : Case : ...

  4. Harmonic Number (调和级数+欧拉常数)题解

    Harmonic Number In mathematics, the nth harmonic number is the sum of the reciprocals of the first n ...

  5. Harmonic Number 求Hn; Hn = 1 + 1/2 + 1/3 + ... + 1/n; (n<=1e8) T<=1e4; 精确到1e-8; 打表或者调和级数

    /** 题目:Harmonic Number 链接:https://vjudge.net/contest/154246#problem/I 题意:求Hn: Hn = 1 + 1/2 + 1/3 + . ...

  6. Harmonic Number (LightOJ 1234)(调和级数 或者 区块储存答案)

    题解:隔一段数字存一个答案,在查询时,只要找到距离n最近而且小于n的存答案值,再把剩余的暴力跑一遍就可以. #include <bits/stdc++.h> using namespace ...

  7. LightOJ - 1234 LightOJ - 1245 Harmonic Number(欧拉系数+调和级数)

    Harmonic Number In mathematics, the nth harmonic number is the sum of the reciprocals of the first n ...

  8. LightOJ 1234 Harmonic Number(打表 + 技巧)

    http://lightoj.com/volume_showproblem.php?problem=1234 Harmonic Number Time Limit:3000MS     Memory ...

  9. LightOJ 1234 Harmonic Number

    D - Harmonic Number Time Limit:3000MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu S ...

随机推荐

  1. Inspector did not run successfully.

    装虚拟机,卡在这个报错1天了, server没有问题,其余所有的agent都不能运行. 这部分没有日志,只有单纯的报错信息,omg,百度.bing一顿骚操作,还是没有解决问题. 因为默认安装jdk1. ...

  2. CSS margin属性取值

    margin表示一个元素的外边距.取值为正值时,表示相对于正常流离邻近元素更远,而取负值时,使其更近 但是,设置margin后,四个方向的表现形式不同 自身发生移动:top.left margin-t ...

  3. Exception类的学习与继承总结

    日期:2018.11.11 星期日 博客期:023 Exception类的学习与继承总结 说起来我们上课还是说过的!老师提到了报错问题出现主要分Exception和Error两类!第一次遇见这个问题是 ...

  4. ionic3 更新打开apk android 8.0报错

    项目中安卓强制更新,当文件下载完.在android 8.0中不能打开apk包. 引入插件报一下错误 import { FileOpener } from '@ionic-native/file-ope ...

  5. 给div拼接html 拼接字符串

    简单描述:拼接html 拼接字符串,说实话,拼接这种东西我自己弄,得花费很多时间,主要是转义字符,单引号,双引号这种小细节调整起来比较麻烦,一旦疏忽多了少了一个符号,页面就有点抽象了,我呢比较粗枝大叶 ...

  6. noip 初赛复习重点知识点

    一.进制转化 将k进制数转化为十进制数: 设k进制数为(abcd)k,则对应十进制数为 (小数同理,乘k的负幂次) 将十进制数转成k进制数: 设十进制数为x: t1=x/k,t2=x mod k t1 ...

  7. CF 833B

    互测题T3... 首先有个dp是非常好想的: 设dp[i][j]为前j个数分成i组的最大得分,则易得:dp[i][j]=max{dp[i-1][k-1]+num[k][j]},其中,num[k][j] ...

  8. 用HBuilderX 打包 vue 项目 为 App 的步骤

    首先打包你的 vue 项目 生成 dist 文件夹,教程请移步  https://www.cnblogs.com/taohuaya/p/10256670.html 看完上面的教程,请确保 你是 将: ...

  9. Python零基础入门之Tkinter的对话框

    这篇博客主要是总结一下Tkinter中的对话框的使用,值得一提的是自从python3.0之后关于关于对话框的模块(messagebox.filedialog.colorchooser)都被收归到了tk ...

  10. 用JAVA写一个简单的英文加密器

    package qhs; import java.util.Scanner; public class JiaM { public static void main(String[] args) { ...