HDU-2161 Primes

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

  

#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;
}

  

posted @ 2011-08-29 21:22  沐阳  阅读(290)  评论(0编辑  收藏  举报