poj 2262 Goldbach's Conjecture 【素数筛】

这题必然的筛选法,出现了2个问题:1.开始开了一个 result 数组(全局变量),想把素数挨个存进来,虽然还计数估算了的,一直的runtime error , 后来发现是多此一举,去掉之后就变成wrong answer,看来可以编译了。这么说来,一百万对于两个大数组还是有点吃不消的  2.筛的时候一定要筛完整,for(j=2*i;j<MAXN;j+=i)prime[j]=1;这里开始用的

j<(MAXN/i),明显就错了。

另外我很好奇题目中说的"Goldbach's conjecture is wrong.",很明显不会wrong的,wrong了科学家们还继续证明干嘛,可是我AC以后看网上的结题报告居然还真有把wrong考虑进去的人。。。

2个错误都存在的代码

#include <stdio.h>

#define MAXN 1000002
#define PrimeNum 671000

int prime[MAXN];		//用筛法求素数,1代表不是素数(被筛掉)
int result[PrimeNum];

int main()
{
	//先打出素数表
	prime[0]=prime[1]=1;    //开始去掉prime[0]和prime[1]
	int i,j;
	for(i=2;i<MAXN;i++)
	{
		if(prime[i]==0)		//如果prime[i]是素数就把他的倍数都筛掉
		{
			for(j=2*i;j<(MAXN/i);j+=i)
				prime[j]=1;
		}
	}

	//test 1
	/*int count=0;
	for(i=2;i<MAXN;i++)
		if(prime[i]==0)
			count++;
	printf("一百万以内的素数有 %d 个\n",count);*/

	//test 2
	/*for(i=2;i<100;i++)
		if(prime[i]==0)
			printf("%d ",i);*/

	int count=0;
	for(i=3;i<100;i++)
		if(prime[i]==0)
		{
			result[count]=i;
			count++;
		}

	/*for(i=0;i<100;i++)
		printf("%d ",result[i]);*/

	int n;
	while(scanf("%d",&n))
	{
		if(n==0)
			break;

		for(i=0; ;i++)
			if(prime[n-result[i]]==0)
			{
				printf("%d = %d + %d\n",n,result[i],n-result[i]);
				break;
			}
	}

	return 0;
}

正确的代码

#include <stdio.h>

#define MAXN 1000002

int prime[MAXN];		//用筛法求素数,1代表不是素数(被筛掉)

int main()
{
	//先打出素数表
	prime[0]=prime[1]=1;    //开始去掉prime[0]和prime[1]
	int i,j;
	for(i=2;i<MAXN;i++)
	{
		if(prime[i]==0)		//如果prime[i]是素数就把他的倍数都筛掉
		{
			for(j=2*i;j<MAXN;j+=i)
				prime[j]=1;
		}
	}

	int n;
	while(scanf("%d",&n))
	{
		if(n==0)
			break;

		for(i=3; ;i++)
			if(prime[i]==0 && prime[n-i]==0)
			{
				printf("%d = %d + %d\n",n,i,n-i);
				break;
			}
	}

	return 0;
}
时间: 2024-12-24 00:47:51

poj 2262 Goldbach&#39;s Conjecture 【素数筛】的相关文章

POJ 2262 Goldbach&amp;#39;s Conjecture

Problem 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 examp

HDOJ 1397 Goldbach&amp;#39;s Conjecture(快速筛选素数法)

Problem Description 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

poj 3006 Dirichlet&amp;#39;s Theorem on Arithmetic Progressions 【素数筛】

说实话,题目很长,但是和真正要思考的东西关系不大... 就用了之前的素数筛的模板,控制了一下输入.输出格式就过了,很水的题,没什么技术含量,我好像也只会用暴搜... #include <stdio.h> #define MAXN 1000002 int prime[MAXN]; //用筛法求素数,1代表不是素数(被筛掉) int main() { //先打出素数表 prime[0]=prime[1]=1; //开始去掉prime[0]和prime[1] int i,j; for(i=2;i&l

LightOJ 1370 Bi-shoe and Phi-shoe(素数筛)

Click Here~~ A - Bi-shoe and Phi-shoe Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu Submit Status Practice LightOJ 1370 Description Bamboo Pole-vault is a massively popular sport in Xzhiland. And Master Phi-shoe is a very popular

poj 2739 Sum of Consecutive Prime Numbers【素数筛】

我觉得这题的数据应该有些刁钻,一共至少提交了五次,一直是WA,无语了......用一个result数组存素数硬是不对.后来就算了,改用直接判断(法二),继续WA,后来发现是MAXN至少应为10002(被数据坑了),终于A掉了...... 后来继续改法一多次,任然WA,一直不清楚原因. 继续思考发现有一个很隐蔽的问题就是我后来用到   if(result[i]>n) break;    而result数组中 10000以内 最后一个素数是 9997,后面全是0了.这样无法停止,所以必须加一个大数作

POJ 2262

 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. B

UVa 640 Self Numbers:类似素数筛

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=581 关键: 01.if (!vis[i]) printf("%d\n", i); 02.vis[d(i)] = true; 完整代码: 01.#include<cstdio> 02.const int maxn = 100000

素数筛【模板】

#include <iostream> #define MAXN 1<<15 using namespace std; int prime[MAXN]; //为0代表是素数 int findPrime() { //先打出素数表 prime[0]=prime[1]=1; int i,j; for(i=2;i<MAXN;i++) { if(prime[i]==0) { for(j=2*i;j<MAXN;j+=i) prime[j]=1; } } return 0; }

poj 1828 Monkeys&amp;#39; Pride 模拟

   排个序,模拟下就好了,水题一个 /* author:jxy lang:C/C++ university:China,Xidian University **If you need to reprint,please indicate the source** */ #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algori