zoj--1951 Goldbach's Conjecture(math)

来源:互联网 发布:mac安装xampp 编辑:程序博客网 时间:2024/05/16 07:35

zoj 1951

题解

验证一百万以内的哥德巴赫猜想。
素数打表,然后枚举。

#include <iostream>#include <cstdio>#include <vector>#include <algorithm>using namespace std;const int maxn = 1000000 + 10;bool  vis[maxn];vector<int> p;int   n;void init(){    for(int i = 2; i < maxn; ++i)    {        if(vis[i]) continue;        p.push_back(i);        for(int j = 2 * i; j < maxn; j += i)            vis[j] = true;    }}int main(){    init();    while(cin >> n && n)    {        for(int i = 0; i < (int)p.size(); ++i)        {            int p1 = p[i], p2 = n - p1;            bool is = binary_search(p.begin(), p.end(), p2);            if(is){                printf("%d = %d + %d\n", n, p1, p2);                break;            }        }    }    return 0;}
0 0
原创粉丝点击