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

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

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



}

全部评论

相关推荐

不愿透露姓名的神秘牛友
05-28 12:15
点赞 评论 收藏
分享
05-29 20:34
门头沟学院 C++
KarlAllen:得做好直接春招的准备。学历差的话,一是面试要求会比学历好的严格不少,二是就算面试通过了也会被排序。总之暑期和秋招对于学历差的就是及其不友好
无实习如何秋招上岸
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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