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

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

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

java实现中最笨的方法了吧🤣🤣🤣
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class Main {
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();
        int tmp = Integer.MAX_VALUE;
        int a = 0, b = 0;
        for(int i=1;i<num;i++){
            for(int j=1;j<=num-i;j++){
                if(i+j==num && su(i) == 1 && su(j) == 1){
                    int c = 0;
                    if(i>+j){
                        c = i-j;
                    }else{
                        c = j-i;
                    }
                    if(c<tmp){
                        tmp = c;
                        a = j;
                        b = i;
                    }
                }
            }
        }
        if(a>b){
            System.out.println(b);
            System.out.println(a);
        }else{
            System.out.println(a);
            System.out.println(b);
        }
    }
    public static int su(int n){
        int count = 0;
        for(int i=1;i<n;i++){
            if(n%i == 0){
                count++;
            }
        }
        if(count>=2){
            return 0;
        }else{
            return 1;
        }
    }
}


#小白的技术进阶日记#
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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