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

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

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

import java.util.Scanner;

import java.util.TreeMap;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            int n = in.nextInt();
            int min=1000,temp=0;
            TreeMap<Integer,Integer> map=new TreeMap<>();
            for(int i=2;i<=n/2;i++){
                if((issu(i)==true)&&(issu(n-i)==true)){
                    temp=n-i-i;
                    if(temp<min){
                        map.put(temp,i);
                        min=temp;
                    }
                }
            }
            System.out.println(map.get(map.firstKey()));
            System.out.println(n-(map.get(map.firstKey())));
        }
    }
    public static boolean issu(int a){
        if(a==2){
            return true;
        }
        for(int i=2;i<a;i++){
            if(a%i==0){
                return false;
            }
        }
        return true;
    }
}

全部评论

相关推荐

点赞 评论 收藏
分享
头顶尖尖的程序员:我是26届的不太懂,25届不应该是找的正式工作吗?为什么还在找实习?大四还实习的话是为了能转正的的岗位吗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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