题目链接
AC代码:
#include#include using namespace std;const int maxn = 1e6+10;//===================int prime[maxn];bool number[maxn];int n,cnt;void isprime(int n){ //1~n的素数 memset(number, true, sizeof number); number[0]=number[1] = false; cnt = 0; for (int i = 2; i <= n; i++){ if (number[i]) prime[cnt++] = i; for (int j = 0; j < cnt&&prime[j] * i <= n; j++){ number[prime[j] * i] = false; if (i%prime[j] == 0) break; } }}int main(){ cin >> n; isprime(n); for (int i = 0; i < cnt; i++){ cout << prime[i] << endl; } return 0;}