题解 | #筛选法求素数#
筛选法求素数
https://www.nowcoder.com/practice/06c3dcc303654ef4926654023eca1e5a
#include <stdio.h> int main() { int n = 0; while (scanf("%d", &n) == 1) { int i = 0; int count = 0; for (i = 2; i <= n; i++) { int flag = 0; int j = 0; for (j = 1; j < n; j++) { if (i % j == 0) { flag++; } } if (flag == 2)//用来判断质数只能被1和本身 { count++; printf("%d ", i); } } printf("\n%d\n",n-1-count); } return 0; }