HDU 2012 素数判定 解题报告

链接:http://acm.hdu.edu.cn/showproblem.php?pid=2012      

      素数判定



Problem Description
对于表达式n^2+n+41,当n在(x,y)范围内取整数值时(包括x,y)(-39<=x<y<=50),判定该表达式的值是否都为素数。
 

Input
输入数据有多组,每组占一行,由两个整数x,y组成,当x=0,y=0时,表示输入结束,该行不做处理。
 

Output
对于每个给定范围内的取值,如果表达式的值都为素数,则输出"OK",否则请输出“Sorry”,每组输出占一行。
 

Sample Input
0 1 0 0
 

Sample Output
OK
本来是一道非常简单的题目,但因为把OK写成了ok果断一直WA,以后一定要ctrl+c,杯具ing···
View Code
1 #include<stdio.h>
2 #include<math.h>
3  int fun(int n)
4 {
5 int f=1,i,m;
6 m=n*n+n+41;
7 for(i=2;i<=sqrt(m);i++)
8 {
9 if(m%i==0)
10 {f=0;break;}
11 }
12 return f;
13 }
14 main()
15 {
16 int a,b,j;
17 while(scanf("%d%d",&a,&b)==2,a||b)
18 {
19 {
20 int c,d,k;
21 c=a>b?a:b;
22 d=a<b?a:b;
23 for(j=d;j<=c;j++)
24 {
25 k=fun(j);
26 if(k==0)
27 break;
28 }
29 printf(k?"OK\n":"Sorry\n");
30 }
31 }
32 }
原文地址:https://www.cnblogs.com/jian1573/p/2012930.html