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[];
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(调和级数+欧拉常数)的更多相关文章
- Harmonic Number(调和级数+欧拉常数)
题意:求f(n)=1/1+1/2+1/3+1/4-1/n (1 ≤ n ≤ 108).,精确到10-8 (原题在文末) 知识点: 调和级数(即f(n))至今没有一个完全正确的公式, ...
- C - Harmonic Number(调和级数+欧拉常数)
In mathematics, the nth harmonic number is the sum of the reciprocals of the first n natural numbers ...
- LightOJ 1234 Harmonic Number 调和级数部分和
题目链接:http://lightoj.com/volume_showproblem.php?problem=1234 Sample Input Sample Output Case : Case : ...
- Harmonic Number (调和级数+欧拉常数)题解
Harmonic Number In mathematics, the nth harmonic number is the sum of the reciprocals of the first n ...
- 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 + . ...
- Harmonic Number (LightOJ 1234)(调和级数 或者 区块储存答案)
题解:隔一段数字存一个答案,在查询时,只要找到距离n最近而且小于n的存答案值,再把剩余的暴力跑一遍就可以. #include <bits/stdc++.h> using namespace ...
- LightOJ - 1234 LightOJ - 1245 Harmonic Number(欧拉系数+调和级数)
Harmonic Number In mathematics, the nth harmonic number is the sum of the reciprocals of the first n ...
- LightOJ 1234 Harmonic Number(打表 + 技巧)
http://lightoj.com/volume_showproblem.php?problem=1234 Harmonic Number Time Limit:3000MS Memory ...
- LightOJ 1234 Harmonic Number
D - Harmonic Number Time Limit:3000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu S ...
随机推荐
- -Dmaven.multiModuleProjectDirectory system property is not set.
1.配置 maven 环境变量 新建系统变量 -> 变量名(N): M2_HOME 变量值(V): D:\apache-maven-3.5.4(改为自己的maven目录) -> 添加 pa ...
- Confluence 6 如何备份存储文件和页面信息
备份的 ZIP 文件包含有 entities.xml,这个 XML 文件包含有 Confluence 的所有页面内容和存储附件的目录. 备份 Zip 文件结构 页面的附件是存储在附件存储目录中的,通过 ...
- OC对象本质
@interface person:NSObject{ @public int _age; } @end @implementation person @end @interface student: ...
- libopencv_highgui.so.2.4.9:对‘TIFFReadRGBAStrip@LIBTIFF_4.0’未定义的引用
make之前加上sudo su重新make即可 http://blog.csdn.net/cfyzcc/article/details/52981467
- 【git】提交代码到远程仓库
看完不用,就是一个字:忘! 之前学了两天git结果今天要用的时候,啥也想不起来.... 场景: 已有远程仓库: git@192.168.1.1:test/test.git 要提交代码到远程仓库的新分支 ...
- 第七周学习总结-C#
2018年8月26日 这个周二突然得知另一位老师留的暑假作业,群文件里早就上传了,我居然一直没翻到那里,要不是同学问作业做完没,我可能开学就要“真●裸考”了
- AI学习吧-结算中心
结算中心流程 在结算中心中,主要是对用户添加到购物车商品的结算,由于用户可能添加了多个课程,但是,结算时会选择性的进行支付.在结算时会选中课程id,和对应的价格策略.在后台,首先会对用户进行校验,验证 ...
- IDEA中每次拷贝一个项目的时候必须标记一下配置文件resources,否则报错
- 20165206 2017-2018-2 《Java程序设计》第三周学习总结
20165206 2017-2018-2 <Java程序设计>第三周学习总结 教材学习内容总结 类:class是关键字,用来定义类. 类声明:例如class People. 对象的声明:类 ...
- IOU和非极大值抑制
如何判断对象检测算法运作良好呢? 一.交并比(Intersection over union,IoU) 是产生的候选框(candidate bound)与原标记框(ground truth bound ...