holiday/8.16/N-HarmonicNumber/Harmonic2.cpp
2022-08-18 10:53:07 +08:00

41 lines
658 B
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//调和级数近似公式f(n)=ln(n)+r+1.0/(2*n)
//r是欧拉常数
//注意此公式只适用于n很大的情况。
#include<cstdio>
#include<algorithm>
#include <cmath>
using namespace std;
#define r 0.57721566490153286060651209//欧拉常数
const int N=1e8;
const int w=1e4;
double a[w+5];
int t,n;
int t1;
double ans;
inline void cun()
{
for(int i=1;i<=w;i++)
{
a[i]=a[i-1]+(1.0/i);
}
}
int main()
{
cun();
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
if(n<=w)
{
printf("Case %d: %.10lf\n",++t1,a[n]);//lf->double 不是llf
}
else
{
ans=log(n)+r+1.0/(2*n);//cmath 中log就是ln
printf("Case %d: %.10lf\n",++t1,ans);
}
}
return 0;
}