解题思路:
复习一下线性筛素数
注意事项:
参考代码:
#include <iostream> #include <vector> #define N 100000 using namespace std; vector<int> primeList; bool checkPrime[N]; void getPrime(int n) { for (int i = 2; i <= n; i++) { if (checkPrime[i] == 0) primeList.push_back(i); for (int j = 0; j < primeList.size() && primeList[j] * i <= n; j++) { checkPrime[primeList[j] * i] = true; if (i % primeList[j] == 0) break; } } } int main(void) { int r = 0; cin >> r; getPrime(r); for (vector<int>::iterator it = primeList.begin(); it < primeList.end(); it++) { if (*it <= r) cout << *it << endl; else if (*it > r) break; } return 0; }
0.0分
0 人评分