HDU-2161 Primes

本文介绍了使用C++实现的一种素数筛选算法,并通过输入外挂的方式优化了读取过程。该算法采用标记非素数的方法来找出指定范围内的所有素数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

  

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

  

转载于:https://www.cnblogs.com/Lyush/archive/2011/08/29/2158874.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值