解题思路:
注意事项:
参考代码:
public class 检查一个数是否为质数_水题 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in);
// 素数表
int[] primarys = new int[100];
primarys[0] = 2;
primarys[1] = 3;
primarys[2] = 5;
primarys[3] = 7;
primarys[4] = 11;
primarys[5] = 13;
primarys[6] = 17;
primarys[7] = 19;
int j = 8;
int num = in.nextInt();
int i;
for (i = 0; i < j; i++) {
if (num % primarys[i] == 0) {
System.out.println("N");
break;
}
}
if (i == j) {
int k;
for (k = 2; k < Math.sqrt(num); k++) {
if (num % k == 0) {
System.out.println("N");
break;
}
}
if(k>=Math.sqrt(num)) {
System.out.println("Y");
primarys[j++] = num;
}
}
}
}
0.0分
0 人评分