题解 | #查找组成一个偶数最接近的两个素数#
查找组成一个偶数最接近的两个素数
https://www.nowcoder.com/practice/f8538f9ae3f1484fb137789dec6eedb9
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import static java.util.stream.Stream.*;
public class Main {
public static void main(String[] args) throws IOException {
//testCompletePack();
testTh();
}
private static void testTh() throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String str;
while ((str = bf.readLine()) != null) {
int parseInt = Integer.parseInt(str);
int min = Integer.MAX_VALUE;
int res1 = 0;
int res2 = 0;
for (int i = 2; i < parseInt; i++) {
if (checkInt(i)) {
int i1 = parseInt - i;
if (checkInt(i1)) {
if (Math.abs(i1 - i) < min) {
min = Math.abs(i1 - i);
res1 = i1;
res2 = i;
}
}
}
}
System.out.println(res2);
System.out.println(res1);
}
}
private static Boolean checkInt(Integer number) throws IOException {
for (int i = 2; i < number; i++) {
if (number % i == 0) {
return false;
}
}
return true;
}
}
查看27道真题和解析