题解 | #查找组成一个偶数最接近的两个素数#
查找组成一个偶数最接近的两个素数
http://www.nowcoder.com/practice/f8538f9ae3f1484fb137789dec6eedb9
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = Integer.valueOf(scan.nextLine().trim());
for (int i = n / 2; i < n; i++) {
int num1 = i;
int num2 = n - i;
if (isValid(num1) && isValid(num2)) {
System.out.println(num2);
System.out.println(num1);
return;
}
}
}
public static boolean isValid(int num) {
for (int i = 2; i <= (int) Math.sqrt(num); i++) {
if (num % i == 0) {
return false;
}
}
return true;
}
}