思路
思路很简单,如果该数为质数,并且输入值 - 该数也为质数,则组成一个质数对。
import java.util.Scanner; public class Main2 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int inputValue = scanner.nextInt(); System.out.println(getCount(inputValue)); } // 主程序 static int getCount(int inputValue) { // 因为质数对不能重复,所以判断一半即可,即 (1,5) 和 (5, 1) 一样 int length = inputValue / 2 + 1; int res = 0; for (int i = 1; i < length; i++) { if (IsPrime(i) && IsPrime(inputValue - i)) { res++; } } return res; } // 判断该值是否为质数 static boolean IsPrime(int inputValue) { int endValue = (int) (Math.sqrt(inputValue) + 1); for (int i = 2; i < endValue; i++) { if (inputValue % i == 0) { return false; } } return true; } }