[Acm2012] primality

Title:
For expression n ^ 2 + n + 41, when n takes an integer value in the (x, y) range (including x, y) (- 39 < = x <y <= 50), it is determined that the expression whether the values are prime numbers.

Input
input multiple sets of data, each one row, x two integers, y composition, when x = 0, y = 0, represents the input end of the line without processing.

Output
For each value within a given range, if the value of the expression are prime, then output "OK", otherwise output "Sorry", each output per line.

Sample Input
0 1
0 0

Sample Output
OK

代码:
#include
using namespace std;
int x,y;
bool prime(int n){
bool falg=true;
for(int i=2;i<=n/2;i++){
if(n%i==0){
falg=false;
break;
}
}
return falg;
}
int main(){

while(scanf("%d%d",&x,&y)!=EOF){
bool result=true;
if(x0&y0){
break;
}
for(int i=x;i<=y;i++){
if(!prime(i*i+i+41)){
result=false;
}
}
if(result){
cout<<“OK”<<endl;
}else cout<<“Sorry”<<endl;
}
return 0;
}

Published 42 original articles · won praise 18 · views 410

Guess you like

Origin blog.csdn.net/weixin_42918559/article/details/104034391