P1304哥德巴赫猜想

题目传送

代码:

#include <bits/stdc++.h>

using namespace std;
#define maxn 44150
int prim[maxn];
void initPrim()
{
    int i,j;
    for(i=2; i<maxn; i++)
    {
        j=i;
        for(j=i*2; j<maxn; j+=i)
        {
            prim[j]=1;
        }
    }
}

int main()
{
    initPrim();
    int n,x=4,y;
    cin>>n;
    while(x<=n)
    {
        for(int i=2; i<maxn; i++)
        {
            if(prim[i]==0)
            {
                y=x-i;
                if(prim[y]==0)
                {
                    cout<<x<<"="<<i<<"+"<<y<<endl;
                    break;
                }
            }
        }
        x+=2;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_38851184/article/details/108767562