题解 | #打印质数表#
打印质数表
https://ac.nowcoder.com/acm/problem/22210
the Sieve of Eratosthenes
#include <stdio.h>
//输入一个自然数N,按质数定义从小到大输出1~N(包含N)中所有的质数
int main()
{
int N;
scanf("%d", &N);
bool isPrime[N+1];
for(int i = 0 ; i < N+1 ; i++)
isPrime[i] = true;
for(int i = 2; i * i <= N; i++){
if(isPrime[i]){
for(int j = i * i; j <= N; j+=i)
isPrime[j] = false;
}
}
for(int i = 2; i < N+1; i++){
if(isPrime[i])
printf("%d ",i);
}
}