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

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

题目链接:https://www.luogu.org/problem/P1579

代码:

#include <bits/stdc++.h>
using namespace std;
int x,i;
int y(int x){   //判断是否是素数;
    for(i=2;i<=sqrt(x);i++){
        if(x%i==0){
            return 1;
        }
    }
    return 0;
}
int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    int n,k,j,t=0,p;
    cin>>n;
    if(y(n-4)==0){   //2是最小是质数,如果该数可以表示成2,2,n即为最小解;
        cout<<"2 "<<"2 "<<n-4<<endl;
    }else{
        for(k=3;k<n-4;k+=2){    //偶数一定不是素数;
        for(j=k;j<n-4;j+=2){
            p = n-j-k;
            if(y(k)+y(j)+y(p)==0){
                cout<<k<<" "<<j<<" "<<p<<endl;
                return 0;
            }
        }
    }
    }
}
posted @ 2019-07-27 16:06  幽灵小一只  阅读(184)  评论(0编辑  收藏  举报