P1579 哥德巴赫猜想(升级版)(数论,枚举,素数,洛谷,java)

洛谷链接:https://www.luogu.com.cn/problem/P1579

在这里插入图片描述
在这里插入图片描述

import java.util.Scanner;
 
public class Main {
	private static Scanner cin;
	
	public static void main(String args[]) throws Exception {
		cin = new Scanner(System.in);
		int n = cin.nextInt();
		if(n<=9 || n>=20000) {
			return;
		}
		for(int i=2;i<n;i++) {
			if(isPrime(i)) {
				for(int j=2;j<n;j++) {
					if(isPrime(j)) {
						if((n-i-j)>1 && isPrime(n-i-j)) {
							System.out.println(String.format("%d %d %d",i,j,n-i-j));
							j = n;
							i = n;
						}
					}
				}
			}
		}
		
		
	}
	
	public static boolean isPrime(int n) {
		boolean ret = true;
		if(1==n) {
			ret = false;
			return ret;
		}
		if(2==n || 3==n || 5==n || 7==n){
			ret = true;
			return ret;
		}
			for(int i = 2;i<=Math.sqrt(n);i++) {
				if(n%i == 0) {
					ret = false;
					break;
				}
			}
			
		return ret;
	}
 
}

发布了68 篇原创文章 · 获赞 26 · 访问量 625

猜你喜欢

转载自blog.csdn.net/weixin_44685629/article/details/103799516