C - 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
坑点:
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(调和级数+欧拉常数)的更多相关文章
- Harmonic Number(调和级数+欧拉常数)
题意:求f(n)=1/1+1/2+1/3+1/4-1/n (1 ≤ n ≤ 108).,精确到10-8 (原题在文末) 知识点: 调和级数(即f(n))至今没有一个完全正确的公式, ...
- LightOJ 1234 Harmonic Number 调和级数部分和
题目链接:http://lightoj.com/volume_showproblem.php?problem=1234 Sample Input Sample Output Case : Case : ...
- Harmonic Number(调和级数+欧拉常数)
In mathematics, the nth harmonic number is the sum of the reciprocals of the first n natural numbers ...
- 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 ...
随机推荐
- (十二)数据库查询处理之Query Execution(1)
(十二)数据库查询处理之Query Execution(1) 1. 写在前面 这一大部分就是为了Lab3做准备的 每一个query plan都要实现一个next函数和一个init函数 对于next函数 ...
- 后端程序员之路 48、memcached
memcached - a distributed memory object caching systemhttp://memcached.org/ Memcached 教程 | 菜鸟教程http: ...
- 《C++ Primer》笔记 第5章 语句
空块的作用等价于空语句. case标签必须是整型常量表达式,default也是一种特殊的case标签. 标签不应该孤零零地出现,它后面必须跟上一条语句或者另外一个case标签. 如果在某处一个带有初值 ...
- 大家最常用的编程论坛是哪个呢,欢迎评论!!掘金16 juejin 简书41 jianshu 博客85 csdn137 csdn
软件编程交流论坛 掘金 16 juejin 简书 41 jianshu 博客 85 cnblogs csdn 137 csdn stackoverflow 0 思否 github 大家最常用的 ...
- HDOJ-4027(线段树+区间更新(每个节点更新的值不同))
Can You answer these queries? HDOJ-4027 这道题目和前面做的题目略有不同.以前的题目区间更新的时候都是统一更新的,也就是更新相同的值.但是这里不一样,这里更新的每 ...
- HDR(高动态范围)
一: 简介 一般来说,当存储在帧缓冲(Framebuffer)中时,亮度和颜色的值是默认被限制在0.0到1.0之间的. 但是如果我们遇上了一个特定的区域,其中有多个亮光源使这些数值总和超过了1.0,又 ...
- ASP.NET Core重复读取Request.Body
//HttpContext context.Request.EnableRewind(); //创建缓冲区存放Request.Body的内容,从而允许反复读取Request.Body的Stream u ...
- FreeBSD ports 基本用法
首先获取portsnap#portsnap fetch extract---------------------------------------使用whereis 查询软件地址如#whereis ...
- WBX24T2X CPEX国产化万兆交换板
WBX24T2X是基于盛科CTC5160设计的国产化6U三层万兆CPEX交换板,提供24路千兆电口和2路万兆光口,采用龙芯 2K1000处理器.支持常规的L2/L3协议,支持Telnet.SNMP ...
- WPF 基础 - 在模板中找元素
1. 在 ControlTemplate 中寻找元素 <Window.Resources> <ControlTemplate x:Key="cTmp"> & ...