LightOJ - 1234 Harmonic Number (调和级数近似和)

题干:

多组输入,给你一个n,求出H(n) = 1 + 1 2 \frac{1}{2} 21 + 1 3 \frac{1}{3} 31 + … + 1 n \frac{1}{n} n1 = ∑ 1 n \sum \frac{1}{n} n1 ,保留八位小数。

思路:

(1)观察公式,可以发现这是一个调和级数求前n项和,因为只保留八位小数,我们可以使用调和级数的近似公式() H(n) = l n ( n ) ln(n) ln(n) + 1 2 ∗ n \frac{1}{2*n} 2n1 + c (c为欧拉常数c=0.57721566490153286060651209)
但这个公式只能在n较大的时候使用,否则会有较明显误差,所以我们对小于1000000的n直接暴力求。

#include <cstdio>
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <cstring>
#define LL long long

using namespace std;
const LL mod = 1e9+7;
const double ol=0.57721566490153286060651209;     //欧拉常数
double shu[1000000];
int main()
{
	LL x;
    int t;
    shu[1]=1;
    for(int i=2;i<1000000;i++)
      shu[i]=shu[i-1]+1.0/i;
    scanf("%d",&t);
    for(int j=1;j<=t;j++){
    	scanf("%lld",&x);
    	if(x<1000000)
    	  printf("Case %d: %.10lf\n",j,shu[x]);
    	else
    	{
    		double ans=log(x)+ol+1.0/(2*x);
    		printf("Case %d: %.10lf\n",j,ans);
    	}
    }
	return 0; 
} 

(2)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值