蓝桥杯之筛法
AC代码:
#include <iostream> #include <cstdio> #include <cstring> using namespace std; #define maxn 100005 int a[maxn],judge[maxn]; int main(void) { freopen("in.txt","r",stdin); memset(judge,0,sizeof(judge)); long long n; int j=0; scanf("%lld",&n); for(int i=2;i<=n;i++) { if(judge[i]==0) a[j++]=i; for(int k=i;k<=n;k+=i) judge[k]=1; } for(int i=0;i<j;i++) printf("%d\n",a[i]); fclose(stdin); return 0; }