hdu 2161 Primes 素数打表

在kuangbin带你飞专题看到的,水了一发,但是wa了一次,T了一次,竟然连素数打表都快不会写了。

而且连求素数时候只需到根号n就可以都忘了,假设有因子m大于√n,那么n/m一定小于√n,所以它在√n前面已经被选出来了。

代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<vector>
#include<set>
#include<string>
#include<algorithm>
#define MAX 0x7fffffff

using namespace std;
int primer[16000];
int main()
{
	int i ,j;
	memset(primer,0,sizeof(primer));
	primer[1] = primer[2] = 1;
	int en = sqrt(16000.0); 
	for(i=2; i <= en; i++)
		if(!primer[i] || i==2)
		{
			for(j=i+i; j<=16000; j+=i)
				primer[j] = 1;
		}
	int n,cnt = 0;
	while(cin >> n, n > 0)
	{
		cnt ++;
		printf("%d: ",cnt);
		if(!primer[n])
			cout << "yes" << endl;
		else
			cout << "no" << endl;
	}
	return 0;
}


你可能感兴趣的:(hdu 2161 Primes 素数打表)