您的位置:首页 > 编程语言 > Go语言

poj 2909 Goldbach's Conjecture

2012-07-14 11:06 169 查看
素数水题:

View Code

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#include<cstring>
#include<vector>
using namespace std;
int prime[10000],cnt=0;
bool hash[100024];
void Prime( )
{
int t = ( int )sqrt( 100000.0 ) + 1;
memset( hash , 0 ,sizeof( hash ) );
for(int i = 3 ; i <= t ; i += 2 )
{
if( !hash[i>>1] )
{
int x = i<<1;
for( int j = i * i; j <= 100001 ; j += x )
{
hash[j>>1] = true;
}
}
}
cnt = 0;
prime[cnt++] = 2;
t = 100000/2;
for( int i = 1; i <=t ; i ++ )
{
if( !hash[i] )
{
prime[cnt++] =i * 2 + 1;
}
}
memset( hash , 0 ,sizeof( hash ) );
for( int i = 0 ; i < cnt; i ++ )
{
hash[prime[i]] = true;
}
}
int Solve( int num )
{
int t =  num/2,sum=0;
for( int i = 0 ;i < cnt ;i ++ )
{
if( prime[i] > t ) break;
else
{
if( hash[num-prime[i]] )
{
sum ++;
}
}
}
return sum;
}
int main(  )
{
Prime();
int num;
while( scanf( "%d",&num ),num )
{
printf( "%d\n",Solve( num ) );
}
//system( "pause" );
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: