题解 | 查找组成一个偶数最接近的两个素数
查找组成一个偶数最接近的两个素数
https://www.nowcoder.com/practice/f8538f9ae3f1484fb137789dec6eedb9
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNextInt()) { // 注意 while 处理多个 case int num = in.nextInt(); int a = 0 ;int b = 0; for(int i =2; i <= num/2 ; i++){ if(i <= num/2 && num-i >= num/2 ){ if (isSushu(i) && Main.isSushu(num -i) ){ a = i; b = num - i ; }; } } System.out.println(a); System.out.println(b); } } //判断是否是素数 private static Boolean isSushu(int n){ int count = 0; for(int i =1; i<=n/2; i++){ if(n % i == 0){ count++; } if(count > 1){ break;} } if(count > 1){return false;} if(count == 1){return true;} return false; } }