70pts

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

yiyang0328 @ 2024-05-26 17:15:22

#include <iostream>  
using namespace std;  
bool isprime(int a){
    for(int i=2;i*i<=a;i++){
        if(a%i==0){
            return 0;
        }
    }
    return 1;
}
int main(){
    int n;
    cin>>n;
    for(int i=3;i<n;i+=2){
        if(isprime(i)){
            for(int j=i;j<n;j+=2){
                if(isprime(j) && isprime(n-i-j) && isprime(i)){
                    cout<<i<<" "<<j<<" "<<n-i-j;
                    exit(0);
                }
            }
        }
    }
}

by zzhhyy1234 @ 2024-07-12 20:23:36

建议

if(isprime(j) && isprime(n-i-j) && isprime(i))

中的isprime(i)可删掉


by zzhhyy1234 @ 2024-07-12 20:52:06

《i从二开始》


by yiyang0328 @ 2024-07-17 12:05:12

@zzhhyy1234 ...


by yiyang0328 @ 2024-07-17 12:09:39

#include <iostream>  
using namespace std;  
bool isprime(int a){
    for(int i=2;i*i<=a;i++){
        if(a%i==0){
            return 0;
        }
    }
    return 1;
}
int main(){
    int n;
    cin>>n;
    for(int i=3;i<n;i+=2){
        if(isprime(i)){
            for(int j=i;j<n;j+=2){
                if(isprime(j) && isprime(n-i-j)){
                    cout<<i<<" "<<j<<" "<<n-i-j;
                    exit(0);
                }
            }
        }
    }
}

然后呢


by 13325359668_189_cn @ 2024-07-21 12:52:08

for循环i从2开始


by yiyang0328 @ 2024-08-13 17:46:04

怎么弄呢 @13325359668_189_cn

#include <iostream>  
using namespace std;  
bool isprime(int a){
    for(int i=2;i*i<=a;i++){
        if(a%i==0){
            return 0;
        }
    }
    return 1;
}
int main(){
    int n;
    cin>>n;
    for(int i=2;i<n;i+=2){
        if(isprime(i)){
            for(int j=i;j<n;j+=2){
                if(isprime(j) && isprime(n-i-j)){
                    cout<<i<<" "<<j<<" "<<n-i-j;
                    exit(0);
                }
            }
        }
    }
}

by 13325359668_189_cn @ 2024-08-13 20:24:43

@yiyang0328 14行改成i<=n,i++ 16行改成j<=n-i(防止超时),j++ 一开始是2,i+=2就成4了


by yiyang0328 @ 2024-08-14 16:19:03

已AC,回关了


|