zoukankan      html  css  js  c++  java
  • 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;
    }
    


     

  • 相关阅读:
    Date类型 方法
    迭代方法和归并函数
    js快速排序方法
    reset
    水平垂直居中
    css清除浮动
    box-shadow
    display---我的第一篇博客
    centos7基础安装
    aws和ufile挂载数据盘EBS
  • 原文地址:https://www.cnblogs.com/lj030/p/3002341.html
Copyright © 2011-2022 走看看