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

坑点:

1.输出第一个样例时可以是1.0000000000

2.对于double类型的数据进行除法时要采用1.0/i的形式,否则结果会有误差

题解:

调和级数公式:f(n) = ln(n) + C + 1.0/2*n;

其中C是欧拉常数其值等于C ≈ 0.57721566490153286060651209;

对于较小的数据公式的误差会比较大,所以对于前10000个数据采用打表的方法来求解

AC代码

 1 #include<iostream>
2 #include<cstdio>
3 #include<cmath>
4 const double C = 0.57721566490153286060651209; //欧拉常数
5
6 using namespace std;
7
8 int main()
9 {
10 double a[10005];
11 int t, n;
12 int flag = 0;
13 scanf("%d", &t);
14 a[0] = 0;
15 for(int i = 1; i <= 10000; i++)
16 {
17 a[i] = a[i-1] + 1.0/i;
18 }
19
20 while(t--)
21 {
22 scanf("%d", &n);
23 if(n <= 10000)
24 printf("Case %d: %.10lf\n", ++flag, a[n]);
25 else
26 {
27 double sum;
28 sum = log(n) + C + 1.0/(2*n); //这里是1.0不然的话算的结果有偏差
29 printf("Case %d: %.10lf\n", ++flag, sum);
30 }
31
32 }
33
34 return 0;
35 }

C - 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. LightOJ 1234 Harmonic Number 调和级数部分和

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

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

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

  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. 更换 grub 主题

    默认的 grub 界面比较简陋 然后突然有想法了,想换个主题 具体操作 1.下载 grub 主题包 去这个地址下载主题(应该是这个地址): https://www.gnome-look.org/bro ...

  2. redis数据结构和对象二

    跳跃表(skiplist) 跳跃表是一种有序数据结构.跳跃表支持平均O(logN),最坏O(N)复杂度的节点查找,大部分情况下,跳跃表的效率可以和平衡树相媲美,并且因为跳跃表的实现比平衡树简单,所有不 ...

  3. R语言低级绘图函数画个温度计

    x <- 1:2 y <- runif(2,0,100) par(mar=c(4,6,2,6)) plot(x,y,type="n",xlim=c(0.5,2.5),y ...

  4. 检查字符串是否包含另一串字符串(c++)

    在c++中检查字符串是否包含另一串字符串,这个本来是我做过的一个算法题,不过最近刚好有个需求让我想到了这个题,就在此记录一下! 使用std::string::findfunction string s ...

  5. Java 读取Word文本框中的文本/图片/表格

    Word可插入文本框,文本框中可嵌入文本.图片.表格等内容.对文档中的已有文本框,也可以读取其中的内容.本文以Java程序代码来展示如何读取文本框,包括读取文本框中的文本.图片以及表格等. [程序环境 ...

  6. windows下MySQL如何完全卸载并安装行的版本

    卸载本地mysql之前,请务必要先将需要的数据库备份 停止mysql 服务 windows键-->搜索服务 找到mysql 服务,并停止他 卸载mysql server 在控制面板--程序 找到 ...

  7. 关于主机不能访问虚拟机的web服务解决

    centos7默认并没有开启80端口,我们只有开启就行 [root@localhost sysconfig]# firewall-cmd --permanent --add-port=3032/tcp ...

  8. WinForm的Socket实现简单的聊天室 IM

    1:什么是Socket 所谓套接字(Socket),就是对网络中不同主机上的应用进程之间进行双向通信的端点的抽象. 一个套接字就是网络上进程通信的一端,提供了应用层进程利用网络协议交换数据的机制. 从 ...

  9. windows如何上传ios app到appstore

    我们在hbuilderx这些开发工具打包好ios app后,需要将这个app提交appstore才能让用户下载安装. 上传IOS APP主要是通过苹果开发者中心来上传,然后借助香蕉云编上传工具来上传就 ...

  10. J. Cole 的 InnoDB 系列 - 1. 学习 InnoDB - 深入探索核心原理之旅

    原文地址:https://blog.jcole.us/2013/01/02/on-learning-innodb-a-journey-to-the-core/,本系列翻译会在其基础上扩展一些 MySQ ...