LightOJ-1234

///题意:求一个数的调和级数;
///思路:在1e8的范围内进行分块打表
#include<cstdio>
using namespace std;
const int maxn=1e8;
const int maxn1=2e6+7;///卡了一部分内存;
int cnt=0;
double a[maxn1];

void init()
{
    double ans=0;
    a[0]=0;///可能它的数比最小的单位还要小,所以将a[0]=0可能出错;
    for(int i=1;i<=maxn;i++)
    {
        ans+=1.0/i;
        if(i%50==0) a[++cnt]=ans;///以50为一个单位进行分块进行累加和;(在50的倍数以前都加过了)
    }
}

int main()
{
    init();
    int n,t;
    scanf("%d",&t);
    for(int cas=1;cas<=t;cas++)
    {
        scanf("%d",&n);
        double ans=a[n/50];
        for(int i=n/50*50+1;i<=n;i++)///累加两个相邻的50的倍数之间的小数;
            ans+=1.0/i;
        printf("Case %d: %.10f\n",cas,ans);
    }
    return 0;
}
///这是网上的一个很骚的版本,欧拉函数的一些的应用,注意这个euler在很小的范围内精度损失较大,所以在前一部分数的时候枚举一部分就好了;
///得到的是一个近似值;所以才有取多少位小数的ope;
/*
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
const int maxn=1e5+7;
double x[maxn];
const double C = 0.57721566490153286060651209;
int cnt=0;
void ac()
{
    x[cnt++] = 0;
    double ans = 0;
    for(int i=1;i<=100000;i++)
    {
        ans += 1.0 / i;
        x[cnt++] = ans;
    }
}
int main(){
    int t;
    scanf("%d",&t);
    ac();
    for(int i=1;i<=t;i++)
    {
        int n;
        scanf("%d",&n);
        double ans = 0;
        if(n<100000) ans = x[n];
        else ans = log(n) + C +1 / (2.0*n);
        printf("Case %d: %.10f\n",i,ans);
    }
    return 0;
}
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值