Harmonic Number(调和级数+欧拉常数)

Harmonic Number

https://vjudge.net/contest/288520#problem/I

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

 

 

欧拉常数:

const double oula=0.57721566490153286060651209;

近似公式:ln(n)+C+1/(2*n)    C++中log即为ln

在n小的时候不精确,需要暴力求解

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define lson l,mid,rt<<1
 4 #define rson mid+1,r,rt<<1|1
 5 #define sqr(x) ((x)*(x))
 6 #define pb push_back
 7 #define eb emplace_back
 8 #define maxn 10000005
 9 #define eps 1e-8
10 #define pi acos(-1.0)
11 #define rep(k,i,j) for(int k=i;k<j;k++)
12 typedef long long ll;
13 typedef pair<int,int> pii;
14 typedef pair<long long,int>pli;
15 typedef pair<int,char> pic;
16 typedef pair<pair<int,string>,pii> ppp;
17 typedef unsigned long long ull;
18 const long long mod=1e9+7;
19 const double oula=0.57721566490153286060651209;
20 using namespace std;
21 
22 int t;
23 
24 int main(){
25    // std::ios::sync_with_stdio(false);
26     int t;
27     cin>>t;
28     ///log(n)+C+1/(2*n)  
29     for(int _=1;_<=t;_++){
30         int n;
31         cin>>n;
32         double ans=0;
33         if(n<=100000)
34             for(int i=1;i<=n;i++){
35                 ans=ans+1.0/i;
36             }
37         else{
38             ans=log(n)+oula+1.0/2/n;
39         }
40         printf("Case %d: %.8f\n",_,ans);
41 
42     }
43 }
View Code

 

posted on 2019-03-19 17:31  Fighting_sh  阅读(219)  评论(0编辑  收藏  举报

导航