题解 | #筛选法求素数#
筛选法求素数
https://www.nowcoder.com/practice/06c3dcc303654ef4926654023eca1e5a
#include <stdio.h>
int main() {
int i, j, k, l = 0,z=0;
int ar[100];
while (scanf("%d", &k) != EOF) {
for (i = 2; i <= k; i++) {
ar[l] = i;
l++;
}
for (i = 0; i < l; i++) {
for (j = i; j < l-1; j++) {
if (ar[i]!=0&&ar[j+1]!=0) {
if (ar[j+1] % ar[i] == 0) {
ar[j+1] = 0;
z++;
}
}
}
}
for (i = 0; i < l; i++) {
if (ar[i] != 0) {
printf("%d ", ar[i]);
}
}
printf("\n%d",z);
}
return 0;
}