Prime Time UVA - 10200

ACM中的精度问题

例题

参考博客

ACM精度问题
Prime Time UVA - 10200

题意

判断 n2+n+41 在n = a,和n = b之间的素数的占比

分析

直接打表进行判断,然后前缀和就行了
关键就是精度问题,注意最后要 加上eps

参考代码


const double eps = 1e-6;

bool Is_Prime(LL n)
{

    LL t = sqrt(n);
    for(int i = 2;i <= t; ++i )
    {
        if(n % i == 0)
            return false;
    }
    return true;
}
LL f(LL n)
{
    return n*n+n + 41;
}
int p[10000+10];
int main(void)
{
    for(int i = 0;i <= 10000; ++i)
    {
        if(Is_Prime(f(i)))
            p[i] = 1;
    }//素数打表
    for(int i = 1;i <= 10000; ++i)
        p[i] += p[i-1];//前缀和
    int a,b;
    while(cin>>a>>b)
    {
        double t = b-a+1;
        double tt;
        if(a==0)
            tt = p[b];
        else
            tt = p[b]-p[a-1];
        printf("%.2f\n",tt/t*100+eps);
    }

    return 0;
}

posted on 2017-09-24 06:43  zzuzxy  阅读(147)  评论(0编辑  收藏  举报

导航