您的位置:首页 > 编程语言 > Go语言

zoj--1951 Goldbach's Conjecture(math)

2016-03-18 23:35 549 查看
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  zoj math easy