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

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

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

全部评论

相关推荐

不愿透露姓名的神秘牛友
昨天 13:05
点赞 评论 收藏
分享
05-16 11:16
已编辑
东华理工大学 Java
牛客73769814...:盲猜几十人小公司,庙小妖风大,咋不叫她去4️⃣呢😁
牛客创作赏金赛
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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