Harmonic Number ______自然数倒数和

1+1/2+1/3+1/4+……+1/n   这个调和级数还没有正确的求解公式。

但是当n很大的时候有个近似的公式:

1+1/2+1/3+1/4+1/5+...+1/n ≈ γ+ln(n)+1/(2*n)
γ是欧拉常数,γ=0.57721566490153286060651209... 
ln(n)是n的自然对数(即以e为底的对数,e=2.71828...)

(在c语言中,ln(n)用log(n)表示函数在math.h(还有一个log10(n),如果遇到loga(b) 则用换底公式log(b)/log(a))里面)

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.

扫描二维码关注公众号,回复: 2322917 查看本文章

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

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e4+5;
//欧拉常数
const double r=0.57721566490153286060651209;//(就用这么长差不多了 拿小本本记着)
double vis[maxn];
int main()
{
	vis[1] = 1;
	for(int i = 2; i <= 2000; i++)//测试时2000可以就没测了搞到1w都不算大吧 
		vis[i] = vis[i-1]+1.0/i;
	int n,ant = 1;
	scanf("%d",&n);
	int a;
	while(n--)
	{
		scanf("%d",&a);
		if(a<=2000)
			printf("Case %d: %.10lf\n",ant++,vis[a]);
		else
			printf("Case %d: %.10lf\n",ant++,(double)log10(a)+r+1.0/(2*a));
	}
	return 0;
}

 

猜你喜欢

转载自blog.csdn.net/nothing_227/article/details/81141549