题解 | #筛选法求素数#
筛选法求素数
https://www.nowcoder.com/practice/06c3dcc303654ef4926654023eca1e5a
#include<iostream> using namespace std; int zhi(int x) { for (int i = 2; i < x; i++) { if (x % i == 0) return 0; } return 1; } int main( ) { int a[99], n, count = 0, c = 2; cin >> n; for (int i = 0; i < n - 1; i++) { a[i] = c; c++; } for (int i = 0; i < n - 1; i++) { if (zhi(a[i])); else a[i] = 0; } for (int i = 0; i < n - 1; i++) { if (a[i] != 0) { cout << a[i] << " "; } else count++; } cout << endl<<count; return 0; }