北航机试-素数

在这里插入图片描述
在这里插入图片描述
仔细。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 10005;
int pnum = 0;
int prime[maxn];
bool mark[maxn] = {0};
void find_prime(){
	for(int i=2;i<maxn;i++){
		if(mark[i]==true)  continue;
		prime[pnum++] = i;
		for(int j=i*i;j<maxn;j+=i){
			mark[j] = true;	
		}
	}	
}
int main(){
	find_prime();
	int n,size,cnt;
	while(scanf("%d",&n)!=EOF){
		size = 0;
		cnt = 0;
		while(prime[size]<n){
			if(prime[size]%10==1){
				if(cnt==0)  {
				      printf("%d",prime[size]);
				      cnt++;
				}
				else  printf(" %d",prime[size]);	
			}
			size++;
		}
		printf("\n");
	}
} 

猜你喜欢

转载自blog.csdn.net/weixin_37762592/article/details/88737720