80分,求助,数据始终过不了

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

凌墨 @ 2019-02-16 16:28:44

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

    }
}

by 凌墨 @ 2019-02-16 16:30:50

输入:10009 输出:3 83 9923~~ 就这个不行~~ ε=(´ο`*)))唉


by 蒟蒻365 @ 2019-02-16 16:38:16

这个输出有啥问题吗


by BCZSX @ 2019-02-16 16:41:24

这是AC代码,你循环里面忘记判断就开始下一层循环了,帮你加了@凌墨

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

by 蒟蒻365 @ 2019-02-16 16:42:40

你的zhishu(i);zhishu(j);这两行有啥意义呢,咋不打if

这样一改就过啦

include<bits/stdc++.h>

using namespace std;

int zhishu(int x){

for(int i=2;i*i<=x;i++){

    if(x%i==0){

        return 0;
    }
}
return 1;

} int main(){

int n;

cin>>n;
if(zhishu(n-4)){
    cout<<"2"<<" "<<"2"<<" "<<n-4;
    return 0;
}
for(int i=3;i<n;i+=2){
    if(!zhishu(i)) continue;
    for(int j=i;j<n;j+=2){
        if(zhishu(j)){ 
            if(zhishu(n-i-j)==1){
                cout<<i<<" "<<j<<" "<<n-i-j;
                return 0;
            } 
        }
    }

}

}


by 凌墨 @ 2019-02-16 20:48:10

Thanks♪(・ω・)ノ(双手合十)


|