Java简便解法,直接从中间开始取
查找组成一个偶数最接近的两个素数
http://www.nowcoder.com/questionTerminal/f8538f9ae3f1484fb137789dec6eedb9
import java.util.*; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()){ int n = sc.nextInt(); int l = n/2; int r = n/2; while(l>2 && r<n-2){ if(helper(l) && helper(r) && l+r==n) break; else { l--; r++; } } System.out.println(l); System.out.println(r); } } private static boolean helper(int n){ if(n<=2) return false; for(int i=2;i<n;i++) if(n%i==0) return false; return true; } }