poj 2262

很基础的一道数学题,不过感觉跑的太慢啊,422ms

#include <iostream>
#include <cmath>
using namespace std;
const int maxn=1000001;
int vis[maxn],p[maxn];
int t;
void prime()
{
	t=0;
	int i,j;
	int n=(int)sqrt(maxn-1);
	for(i=2;i<=n;i++)
	{
		if(!vis[i])
		{
			p[t++]=i;
			for(j=i*i;j<=maxn-1;j+=i)
				vis[j]=1;
		}
	}
}
int main()
{
	int n;
	prime();
	while(cin>>n&&n!=0)
	{
		int a,b;
		int i,j=0;
		bool flag1=true,flag2=true;
		for(i=p[j];i<=(n/2);i=p[++j])
		{
			if(!vis[n-i])
			{
				flag1=flag2=false;
				cout<<n<<" = "<<i<<" + "<<n-i<<endl;
				break;
			}
		}
		if(flag1)
		{
			j=t-1;
			for(i=p[j];;i=p[--j])
			{
				if(!vis[n-i])
				{
					cout<<n<<" = "<<i<<" + "<<n-i<<endl;
					flag2=false;
					break;
				}
			}
		}
		if(flag2)
			cout<<"Goldbach's conjecture is wrong." <<endl;
	}
	return 0;
}


 

posted @ 2012-11-09 17:11  LJ_COME!!!!!  阅读(128)  评论(0编辑  收藏  举报