现在的位置: 首页 > 综合 > 正文

HDU-2161 Primes

2012年12月25日 ⁄ 综合 ⁄ 共 525字 ⁄ 字号 评论关闭

  这里主要预习了素数筛选以及输入外挂的编写。涛神的筛选法一直记着,给力。

  

#include <cstdio>
#include
<cstring>
#include
<cstdlib>
using namespace std;

char hash[16005] = {0};

bool getint( int &t )
{
char c; int f = 1;
while( c = getchar(), c != '-' && ( c < '0' || c > '9' ) )
{
if( c == EOF )
return false;
}
if( c == '-' )
f
= -1;
else
t
= c - '0';
while( c = getchar(), c >= '0' && c <= '9' )
t
= t * 10 + c -'0';
t
*= f;
return true;
}

int main()
{
hash[
1] = 1;
for( int i = 2; i <= 16000; i += 2 )
hash[i]
= 1;
for( int i = 3; i <= 301; ++i )
{
int k = 2 * i;
for( int j = i * i; j <= 16000; j += k )
{
hash[j]
= 1;
}
}
int N, t = 1;
while( getint( N ), N> 0 )
{
printf( hash[N]
? "%d: no\n" : "%d: yes\n", t );
t
++;
}
return 0;
}

  

抱歉!评论已关闭.