HDU_oj_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
 
分析:就是判断素数
注意点:
 
 
 1 #include<iostream>
 2 using namespace std;
 3 
 4 int fun(int x)
 5 {
 6     for(int i=2;i*i<=x;i++)
 7     {
 8         if(x%i==0)
 9         return 0;
10     }
11     return 1;
12 }
13 int main()
14 {
15     int x,y,z;
16     int flag;
17     
18     while(cin>>x>>y)
19     {
20         if(x==0&&y==0)
21         continue;
22         flag=0;
23         for(;x<=y;x++)
24         {
25             z=x*x+x+41;
26             if(!fun(z))
27             {
28                 cout<<"Sorry"<<endl;
29                 flag=1;
30                 break;
31             }
32         }
33         if(!flag)
34         cout<<"OK"<<endl;
35     }
36 }

后来看到有人投机取巧:

这题仅当 n取 40、41、44、49时输出Sorry,于是就出现了判断x和y范围内是不是含有这几个特殊值来解答这道题。

原文地址:https://www.cnblogs.com/tenjl-exv/p/7967563.html