首页 > ACM题库 > HDU-杭电 > HDU 1397 Goldbach’s Conjecture-最小生成树-[解题报告] C++
2013
12-09

HDU 1397 Goldbach’s Conjecture-最小生成树-[解题报告] C++

Goldbach’s Conjecture

问题描述 :

Goldbach’s Conjecture: For any even number n greater than or equal to 4, there exists at least one pair of prime numbers p1 and p2 such that n = p1 + p2.
This conjecture has not been proved nor refused yet. No one is sure whether this conjecture actually holds. However, one can find such a pair of prime numbers, if any, for a given even number. The problem here is to write a program that reports the number of all the pairs of prime numbers satisfying the condition in the conjecture for a given even number.

A sequence of even numbers is given as input. Corresponding to each number, the program should output the number of pairs mentioned above. Notice that we are interested in the number of essentially different pairs and therefore you should not count (p1, p2) and (p2, p1) separately as two different pairs.

输入:

An integer is given in each input line. You may assume that each integer is even, and is greater than or equal to 4 and less than 2^15. The end of the input is indicated by a number 0.

输出:

Each output line should contain an integer number. No other characters should appear in the output.

样例输入:

6
10
12
0

样例输出:

1
2
1

#include "stdio.h"
#include <math.h>
#define MAXINT 65536
int main()
{
	bool prime[MAXINT];
	int i, l, j, n;
	//构造素数表
	for (i = 0; i < MAXINT; i++)
		prime[i] = 1;
	l = sqrt((double)MAXINT);
	for(i = 2; i <= l; i++)
		if(prime[i])
			for(j = 2; j*i < MAXINT; j++)
				prime[j*i] = 0;
	//构造素数表
    while(~scanf("%d",&n))
	{
		if(n == 0) break;
		l = n/2, j = 0;
		for (i = 2; i <= l; i++)
			if (prime[i] && prime[n-i])
				j++;
        printf("%d\n", j);
    }
    
    return 0;
}

解题报告转自:http://blog.csdn.net/ysc504/article/details/8251210


  1. bottes vernies blanches

    I appreciate the efforts you men and women place in to share blogs on such sort of matters, it was certainly useful. Keep Posting!

  2. Excellent Web-site! I required to ask if I might webpages and use a component of the net web website and use a number of factors for just about any faculty process. Please notify me through email regardless of whether that would be excellent. Many thanks

  3. Often We don’t set up on weblogs, but I would like to condition that this established up really forced me individually to do this! considerably outstanding publish

  4. 给你一组数据吧:29 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 1000。此时的数据量还是很小的,耗时却不短。这种方法确实可以,当然或许还有其他的优化方案,但是优化只能针对某些数据,不太可能在所有情况下都能在可接受的时间内求解出答案。