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 }

最新文章

  1. C语言中的system函数参数及其作用
  2. EC笔记:第二部分:12、复制对象时勿忘其每一个成分
  3. java问题排查可能用到的一些命令
  4. Lucene教程(转)
  5. sqlmap我常用到的几个参数
  6. 4、C#基础整理(if语句经典习题)
  7. 【LeetCode】9 &amp; 234 &amp; 206 - Palindrome Number &amp; Palindrome Linked List &amp; Reverse Linked List
  8. how to make choices about girls?
  9. flex与js相互调用
  10. Python3基础 用 函数递归求解 一个数字的阶乘
  11. Visual Studio 2017十五项新功能体验
  12. 6.26学习 异步委托回调函数 VS 多线程 VS 并行处理
  13. Spring MVC的handlermapping之RequestMappingHandlerMapping初始化
  14. [NOIp 2014]解方程
  15. markdown绘图插件----mermaid简介
  16. 机器学习之隐马尔科夫模型HMM(六)
  17. shell 不使用循环批量创建用户
  18. Maven配置国内镜像仓库
  19. BFGS算法(转载)
  20. this和判断的位置对赋值的要求

热门文章

  1. 《C++ Primer》笔记 第2章 变量和基本类型
  2. 【转载】Java虚拟机类加载机制与案例分析
  3. 《Selenium自动化测试实战》新书上市,有需要朋友们可以了解下,欢迎大家多提宝贵意见
  4. C# webapi跨域
  5. Linux下查看文件内容的几种常用命令
  6. git的回滚与撤销【reset and revert】
  7. Spring Boot 启动过程
  8. java实现所有排序算法
  9. 计算机体系结构——CH2 指令系统
  10. 基于react hooks,zarm组件库配置开发h5表单页面