UVA 10200 Prime Time【暴力,精度】

题目链接:

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1141

题意:

给定区间,求区间内所有整数a,f(a) = a * a + a - 1为质数的概率。

分析:

卡精度卡的蛋疼。。
最后要加个eps处理四舍五入问题。。不是很懂。。

代码:

#include<cstdio>
const int maxm = 1e4 + 5;
double eps = 1e-6;
int cnt[maxm];
bool prime(int a)
{
    for(int i = 2; i * i <= a; i++){
        if(a % i == 0) return false;
    }
    return true;
}
void getans()
{
    for(int i = 0; i < maxm; i++){
        int ans = i * i + i + 41;
        cnt[i] = cnt[i - 1] + prime(ans);
    }
}
int main (void)
{
    int a, b;
    getans();
    while(~scanf("%d%d", &a, &b)){
        int ans = cnt[b] - cnt[a - 1];
        printf("%.2f\n", ans * 100.0 / (b - a + 1) + eps);
    }
    return 0;
}
posted @ 2016-04-13 13:00  zhuyujiang  阅读(121)  评论(0编辑  收藏  举报