蓝桥杯之筛法

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;
}

 

posted @ 2019-02-27 06:38  pha创噬  阅读(218)  评论(0编辑  收藏  举报