1 条题解

  • 0
    @ 2023-6-11 12:22:24

    C++ :

    #include<bits/stdc++.h>
    using namespace std;
    int SS(int n)
    {
    	if(n < 2)
    	{
    		return 0;
    	}
    	for(int i = 2;i * i <= n;i++)
    	{
    		if(n % i == 0)
    		{
    			return 0;
    		}
    	}
    	return 1;
    }
    int main()
    {
    	int a;
    	cin >> a;
    	for(int i = 2;i <= a;i++)
    	{
    		for(int j = 2;j <= a;j++)
    		{
    			if(SS(i) == 1 && SS(j) == 1 && SS(a - i - j) == 1)
    			{
    				cout << i << " " << j << " " << a - i - j;
    				return 0;
    			}
    		}
    	}
    	return 0;
    }
    
    

    Java :

    import java.util.Scanner;
    
    public class Main {
    	private static boolean[] flags = new boolean[20001];
    	private static int n;
    
    	private static void init() {
    		flags[0] = flags[1] = true;
    		for (int i = 2; i < flags.length; i++) {
    			if (!flags[i]) {
    				for (int j = 2 * i; j < flags.length; j += i) {
    					flags[j] = true;
    				}
    			}
    		}
    	}
    
    	public static void main(String[] args) {
    		Scanner scanner = new Scanner(System.in);
    
    		n = scanner.nextInt();
    
    		init();
    		int[] f = new int[3];
    
    		for (int i = 2; i <= n; i++) {
    			if(!flags[i])
    			f[0] = i;
    			else {
    				continue;
    			}
    			for (int j = 2; j <= n; j++) {
    				if(!flags[j])
    					f[1] = j;
    					else {
    						continue;
    					}
    				for (int k = 2; k <=n; k++) {
    					if (!flags[k]&&f[0] + f[1] + k == n) {
    						System.out.println(f[0] + " " + f[1] + " " + k);
    						return;
    					}
    				}
    			}	
    		}
    		
    	}
    
    }
    
    
    • 1

    信息

    ID
    2748
    时间
    1000ms
    内存
    16MiB
    难度
    (无)
    标签
    递交数
    0
    已通过
    0
    上传者