import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
//在此输入您的代码...
for (int i = 53; i <=100000000 ; i++) {
if (test(i)){
System.out.println(i);
}
}
// if (test(100)){
// System.out.println(100);
// }
scan.close();
}
private static boolean test(int i) {
String str = i+"";
for (int j = 0; j <str.length() ; j++) {
for (int k = j; k <str.length(); k++) {
int num = Integer.parseInt(str.substring(j,k+1));
if (!check(num)){
return false;
}
}
}
return true;
}
private static boolean check(int num) {
if (num == 1 || num ==0) return false;
for (int i = 2; i <=Math.sqrt(num) ; i++) {
if (num %i==0){
return false;
}
}
return true;
}
}
答案是373
总结如下
for (int j = 0; j <str.length() ; j++) {
for (int k = j; k <str.length(); k++) {
int num = Integer.parseInt(str.substring(j,k+1));
if (!check(num)){
return false;
}
}
}
public class Test {
public static void main(String[] args) {
String str = 123456+"";
for (int j = 0; j <str.length() ; j++) {
for (int k = j+1; k <str.length(); k++) {
int num = Integer.parseInt(str.substring(j,k));
System.out.println(num);
}
}
}
}
for (int j = 0; j <str.length() ; j++) {
for (int k = j; k <str.length(); k++) {
int num = Integer.parseInt(str.substring(j,k+1));
if (!check(num)){
return false;
}
}
}
return true;
最后再返回为true
因为只有所有子串遍历以后发现都是false,才能确定是true