题解 | #查找组成一个偶数最接近的两个素数#
查找组成一个偶数最接近的两个素数
http://www.nowcoder.com/practice/f8538f9ae3f1484fb137789dec6eedb9
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextInt()) {
int n = sc.nextInt();
int left = n / 2;
int right = n / 2;
while (left >= 2) {
if (isPrime(left) && isPrime(right) && left + right == n) {
break;
} else {
left--;
right++;
}
}
System.out.println(left);
System.out.println(right);
}
}
public static boolean isPrime(int n) {
if (n < 2) {
return false;
}
for(int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
}