POJ2262 Goldbach's Conjecture

Description

In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in which he made the following conjecture: 
Every even number greater than 4 can be 
written as the sum of two odd prime numbers.

For example: 
8 = 3 + 5. Both 3 and 5 are odd prime numbers. 
20 = 3 + 17 = 7 + 13. 
42 = 5 + 37 = 11 + 31 = 13 + 29 = 19 + 23.

Today it is still unproven whether the conjecture is right. (Oh wait, I have the proof of course, but it is too long to write it on the margin of this page.) 
Anyway, your task is now to verify Goldbach's conjecture for all even numbers less than a million. 

Input

The input will contain one or more test cases. 
Each test case consists of one even integer n with 6 <= n < 1000000. 
Input will be terminated by a value of 0 for n.

Output

For each test case, print one line of the form n = a + b, where a and b are odd primes. Numbers and operators should be separated by exactly one blank like in the sample output below. If there is more than one pair of odd primes adding up to n, choose the pair where the difference b - a is maximized. If there is no such pair, print a line saying "Goldbach's conjecture is wrong."
 
题意:就是让你把某个数拆分成两个两个质数相加的形式从而证明哥德巴赫猜想,如果不能证明就输出,哥德巴赫猜想是错的。
 
题解:我是先把数据范围内的质数都用欧拉筛筛了出来,然后枚举一下,如果要拆分的数减去某一个质数后的数也是质数,就输出。对于第二种情况,
显然不存在就不用管了,注意输出格式,这道题一开始我的评测显示“Presentation Error”,后来上网查是输出格式错误,长知识哦。
后来在网上看到有人用试除法做也A了,好水哦。
 
 
 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<algorithm>
 5 
 6 using namespace std;
 7 
 8 int n,is_prime[1000005],prime[1000005],cnt;
 9 
10 inline void ou_prime(int x)
11 {
12     for(int i=2;i<=x;i++)
13     {
14         if(is_prime[i]==0) is_prime[i]=i,prime[++cnt]=i;
15         for(int j=1;j<=cnt;j++)
16         {
17             if(i*prime[j]>x||is_prime[i]<prime[j]) break;
18             is_prime[i*prime[j]]=prime[j];
19         }
20     }
21 }
22 
23 int main()
24 {
25     ou_prime(1000000);
26     while(1)
27     {
28         scanf("%d",&n);
29         if(n==0) return 0;
30         for(int i=1;i<=cnt;i++)
31            if(is_prime[n-prime[i]]==n-prime[i])
32            {
33                    printf("%d = %d + %d
",n,prime[i],n-prime[i]);
34                    break;
35            }
36     }
37     return 0;
38 }
原文地址:https://www.cnblogs.com/Hoyoak/p/11384927.html