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. 基于SaaS平台的iHRM项目的前端项目介绍

    1.下载安装node.js 访问https://nodejs.org/en/,然后下载安装即可 2. 查看是否安装成功 打开cmd命令行,输入node -v 如果出现对应的版本号,即为安装成功 3.从 ...

  2. 500GJava/Hadoop/Spark/机器学习...视频教程免费分享 百度云持续更新

    参加工作这么长时间了,工作中遇到了不少技能都是看视频教程学习的,相比较看书而言看视频确实比较容易理解.分享一下自己看过的和收集的视频教程. 资源包括: 大数据方面的Hadoop(云帆,小象学院,八斗学 ...

  3. kubernetes cpu限制参数说明

    docker CPU限制参数 Option Description --cpus=<value> Specify how much of the available CPU resourc ...

  4. 【转载】关于grad_tensors的解惑

    转载:https://www.cnblogs.com/marsggbo/p/11549631.html 平常都是无脑使用backward,每次看到别人的代码里使用诸如autograd.grad这种方法 ...

  5. 【pytest官方文档】解读fixtures - 8. yield和addfinalizer的区别(填坑)

    在上一章中,文末留下了一个坑待填补,疑问是这样的: 目前从官方文档中看到的是 We have to be careful though, because pytest will run that fi ...

  6. vs - 调试的技巧

    在自助和局部变量窗口中固定属性 https://docs.microsoft.com/zh-cn/visualstudio/debugger/autos-and-locals-windows?view ...

  7. python基础学习之函数基础和部分内置函数

    在函数调用的时候,必备参数必须要传入 函数定义: def  函数名: 代码块pass return  返回值 函数名命名规则: 字母.数字和下划线组成,和变量命名规则一致 pass在这里表示什么都没有 ...

  8. 【体系结构】Oracle进程架构

    Client Process的介绍 Client and Server Processes Client Process代表着客户端进程,每一个客户端进程关联着一个Server Process(服务器 ...

  9. pwnable.kr第二题collision

    1 col@prowl:~$ ls -al 2 total 36 3 drwxr-x--- 5 root col 4096 Oct 23 2016 . 4 drwxr-xr-x 114 root ro ...

  10. Git本地仓库基本操作-1

    code[class*="language-"], pre[class*="language-"] { color: rgba(51, 51, 51, 1); ...