题解 | #查找组成一个偶数最接近的两个素数#

查找组成一个偶数最接近的两个素数

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;
    }



}

全部评论

相关推荐

政委qqq:这道题在算法竞赛里唯一考的就是高精度,但是只能难住C++这类语言,Python直接a+b秒天秒地
点赞 评论 收藏
分享
米哈游校招进主页喵:大佬 考虑我司不 考虑的话可以看我主页帖子
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务