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.
1、这个精度就不要想直接能循环出来了,然后感觉这个形式很眼熟啊,不就是得到欧拉常数的那个级数,于是就搜了公式和欧拉常数愉快的带进去了
公式:
f(n)≈ln(n)+C+12n
2、一般这个公式记不住啊,没办法还是打表吧。说是隔40个数记录一次就行。
#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <queue>
#include <cmath>
#define maxn 2500005
using namespace std;
double a[maxn]={0.0,1.1};
int main()
{
int T, t;
double s=1.0;
for(int i=2; i<=100000000; i++)
{
s+=1.0/i;
if(i%40==0)a[i/40]=s;
}
while(~scanf("%d", &T)){
for(t=1; t<=T; t++){
int n;
scanf("%d", &n);
int x=n/40;
s=a[x];
for(int j=40*x+1; j<=n; j++)
s+=1.0/j;
printf("Case %d: %.10f\n", t, s);
}
}
return 0;
}