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

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

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

判断素数要会写哦

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        while(scan.hasNext()){
            int n = scan.nextInt();
            System.out.println(getCount(n));
            System.out.println(n-getCount(n));
        }
        scan.close();
    }
    
    public static boolean isPrime(int n){
        for(int i = 2; i<=Math.sqrt(n); i++){
            if(n%i == 0){
                return false;
            }
        }
        return true;
    }
    
    public static int getCount(int n){
        int i = n/2;
        int j = n/2;
        while(!isPrime(i) || !isPrime(j)){
            i--;
            j++;
        }
        return i;
    }
}











全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务