题解 | 素数判断
素数判断
https://www.nowcoder.com/practice/5ab1b9690af047699e96c87dee65def4
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextInt()) { // 注意 while 处理多个 case
int T = in.nextInt();
for(int i = 0; i < T; i++){
int n = in.nextInt();
boolean panduan = flag(n);
print(panduan);
}
}
}
public static boolean flag(int n){
if(n <= 1){
return false;
}else if(n == 2){
return true;
}else{
for(int j = 2; j <= Math.sqrt(n); j++){
if(n % j == 0){
return false;
}
}
return true;
}
}
public static void print(boolean flag){
if(flag == true){
System.out.println("Yes");
}else{
System.out.println("No");
}
}
}

阿里云工作强度 694人发布