当前位置: 代码迷 >> 综合 >> 综合实践九1000判定素数AC
  详细解决方案

综合实践九1000判定素数AC

热度:87   发布时间:2023-10-15 19:59:51.0

素数判定

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 415   Accepted Submission(s) : 191
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


#include<iostream>

using namespace std;
int main()
{
int min,max,a,x,i,j;
int s;


while (cin >> min >> max)
{
int y = 0, count = 0;//位置挺重要
if (min != 0 || max != 0){
for (x = min; x <= max; x++)
{

y++;
s = x*x + x + 41;
for (j = 2; j <= s; j++)
{
if (s != j&&s%j == 0)
break; 
if (s == j)
count++;
}
}

if (count == y)
cout << "OK" << endl;
if (count != y)
cout << "Sorry" << endl;

}
}
//system("pause");
return 0;
}
  相关解决方案