题解 | #筛选法求素数#
筛选法求素数
https://www.nowcoder.com/practice/06c3dcc303654ef4926654023eca1e5a
#include <stdio.h> int main() { int n = 0; while (scanf("%d", &n) != EOF) { int arr[101]; int i = 0; //存储数据 for (i = 2; i <= n; i++) { arr[i] = i; } int j = 0; for (j = 2; j <= n; j++) { int k = 0; for (k = j + 1; k <= n; k++) { if (arr[k] % j == 0) { arr[k] = 0; } } } int count = 0; for (i = 2; i <= n; i++) { if (arr[i] != 0) printf("%d ", arr[i]); else count++; } printf("\n%d\n", count); } return 0; }
c语言刷题 文章被收录于专栏
c语言刷题题目