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

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

https://www.nowcoder.com/practice/f8538f9ae3f1484fb137789dec6eedb9

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner fzhinput = new Scanner(System.in);
        int sz = fzhinput.nextInt();
        int s1 = -1, s2 = -1;
        int qs1 = -1;
        int qs2 = -1;
        int minDifference = 10000;
        boolean zspd1 = false;
        boolean zspd2 = false;
        int num =sz/2;
        for (s1 = 1; s1 <= num; s1++) {
            zspd1 = false;
            zspd2 = false;
            for (int i = 2; i < s1; i++) {
                if (s1 % i == 0) {
                    zspd1 = true;
                }
            }
            if (zspd1 == false) {
                s2 = sz - s1;
                for (int i = 2; i < s2; i++) {
                    if (s2 % i == 0) {
                        zspd2 = true;
                    }
                }
                if (zspd1 == false && zspd2 == false) {
                    qs1 = s1;
                    qs2 = s2;
                }

            }
            if (qs1 > 0 && qs2 > 0) {
                int min = qs2 - qs1;
                if (min < minDifference) {
                    minDifference = min;
                }
            }

        }
        int sz1 = (sz - minDifference) / 2;
        int sz2 = (sz - minDifference) / 2 + minDifference;
        System.out.println(sz1);
        System.out.println(sz2);
    }
}

全部评论

相关推荐

11-05 07:29
贵州大学 Java
点赞 评论 收藏
分享
Noob1024:一笔传三代,人走笔还在
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务