UVA 10200

来源:互联网 发布:javascript用什么打开 编辑:程序博客网 时间:2024/06/06 23:53

Problem Description

Euler is a well-known matematician, and, among many other things, he discovered that the formulan2 + n+41 produces a prime for 0 ≤ n < 40. For n = 40, the formula produces 1681, which is 41 ∗ 41.Even though this formula doesn’t always produce a prime, it still produces a lot of primes. It’s knownthat for n ≤ 10000000, there are 47,5% of primes produced by the formula!So, you’ll write a program that will output how many primes does the formula output for a certaininterval.InputEach line of input will be given two positive integer a and b such that 0 ≤ a ≤ b ≤ 10000. You mustread until the end of the file.OutputFor each pair a, b read, you must output the percentage of prime numbers produced by the formula inthis interval (a ≤ n ≤ b) rounded to two decimal digits.

Sample Input

0 39

0 40

39 40

Sample Output

100.00

97.565

0.00


题解:注意高精度,最后加个1e-8就行了。(不知道为啥->_->)


<span style="font-family:SimSun;">#include<cstdio>int s[10010];int prime(int n){for(int i=2;i*i<=n;i++)if(n%i==0)return 1;return 0;}int main(){for(int i=0;i<=10000;i++)s[i]=prime(i*i+i+41);double a,b,sum;while(~scanf("%lf%lf",&a,&b)){sum=0;for(int i=a;i<=b;i++){if(!s[i])sum+=1;}double v;v=sum/(b-a+1)*100;printf("%.2lf\n",v+1e-8);//高精度,不加不对。 }return 0;}</span>


0 0