萌新求助 麻烦大犇们看看哪里错了

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

某Dong @ 2017-10-09 09:22:48

#include <iostream>
#include <algorithm>
using namespace std;

bool f=0;
int ele[3]={};

bool prime(int n){
    bool flag=1;
    if(n==1||n==0)flag=0;
    for(int i=2;i*i<=n;i++){
        if(n%i==0) {
            flag=0;
            break;
        }
    }
    return flag;
}

void guess(int num,int time){
    if(f||time>2) return;

    if(time==2){
        if(prime(num)){
            f=1;
            ele[2]=num;
        }
            return;
    }

    if(time<2){
        for(int i=2;i<num;i++){
            if(prime(i)){
                ele[time]=i;
                num-=i;
                time++;
//                    cout<<i<<" "<<num<<" "<<time<<endl;
                guess(num,time);    
            }
        }
    }

}

int main(){
    int n;
    cin>>n;
    guess(n,0);
    cout<<ele[0]<<" "<<ele[1]<<" "<<ele[2]<<" ";

} 

by 起名真的很难 @ 2017-10-09 12:33:13

首先,你的判断素数虽然没错,但是写的过于复杂


bool prime(int n)
{
If(n<2)return 0;
for(int i=1;i\*i<=n;i++)
If(n%i==0)return 0;
return 1;
}

by 起名真的很难 @ 2017-10-09 12:35:50

还有,一个暴力就可以过的程序,活生生的变得这么复杂


by 起名真的很难 @ 2017-10-09 12:37:13


#include<bits/stdc++.h>
using namespace std;
int n;
int prime(int n)
{
    for(int i=2;i<=sqrt(n);i++)
    if(!(n%i))return 0;
    return 1;
}
int main()
{
    scanf("%d",&n);
    for(int i=2;i<=n/3;i++)
        for(int j=i;j<=n/3;j++)
            if(prime(i)&&prime(j)&&prime(n-i-j))
            {printf("%d %d %d",i,j,n-i-j);return 0;}
}

|