import java.util.Scanner;
/**
*
* @solution : Bestcoder Pro.ID 2012 素数判定
* @discription :
* 对于表达式n^2+n+41,当n在(x,y)范围内取整数值时(包括x,y)(-39<=x<y<=50),判定该表达式的值是否都为素数
* 。
* @input : 输入数据有多组,每组占一行,由两个整数x,y组成,当x=0,y=0时,表示输入结束,该行不做处理。
* @output : 对于每个给定范围内的取值,如果表达式的值都为素数,则输出"OK",否则请输出“Sorry”,每组输出占一行。
*
* @file com.acmcoder.acm.ProblemID2012.Main.java
* @author wuyanhui
* @version Apr 22, 2016 -- 7:24:14 PM
*
* @PS :
*/
public class Main {
public static void main(String[] args) {
@SuppressWarnings("resource")
Scanner in = new Scanner(System.in);
int x, y;
boolean isPrimes;
while (in.hasNext()) {
x = in.nextInt();
y = in.nextInt();
if (x == 0 && y == 0)
return;
isPrimes = true;
for (int i = x; i <= y && isPrimes; i++) {
if (!isPrime(i * i + i + 41)) {
isPrimes = false;
}
}
System.out.println(isPrimes ? "OK" : "Sorry");
}
in.close();
}
/**
* 判断是否为素数
*/
public static boolean isPrime(int value) {
boolean isPrime = true;
for (int i = 2; isPrime && i < value / 2; i++) {
if (value % i == 0) {
isPrime = false;
}
}
return isPrime;
}
}
Bestcoder Pro.ID 2012 素数判定
最新推荐文章于 2023-12-30 16:56:33 发布