30分求助!!!

P1579 哥德巴赫猜想(升级版)

Dreams_Knight @ 2023-02-02 20:42:13

#include<bits/stdc++.h>
using namespace std;
bool is_prime(int x){
    if(x == 1){
        return false;
    }
    for(int i = 2;i <= sqrt(x);i++){
        if(x % i == 0){
            return false;
        }
    }
    return true;
}
int main(){
    int n;
    cin >> n;
    for(int i = 2;i < n - 1;i++){
        if(is_prime(i) and is_prime(i) and is_prime(n - i - i) and i + i + (n - i - i) == n){
                cout << i << " " << i << " " << n - i - i;
                return 0; 
        }
    }
} 

by InversionShadow @ 2023-02-02 20:45:42

@Dreams_Promise 第一个质数和第二个质数可能不一样


by Dreams_Knight @ 2023-02-02 20:53:16

@yuandingquan1101问一下该怎么改正???


by ouchenchen1 @ 2023-04-22 22:31:08

@Dreams_Promise 套两层循环呗

for(int i = 2;i <= n;i++){
   for(int j = 2;j < n - i; j ++){
        int k = n - i - j;                      if(is_prime(i) and is_prime(j) and is_prime(k)){
    cout << i << " " << j << " " << k;
   return 0; 
        }
}
    }

|