暑假选拔赛01 ---- Prime

来源:互联网 发布:天启软件 编辑:程序博客网 时间:2024/05/17 09:44

Problem Description

按从小到大顺序输出[1,6000000]内的所有质数,每个数一行。

Input

没有输入。

Output

如上。 (样例只是给出了答案的头几行)

Sample Input

Sample Output

2
3
5
……

解题思路

参考 素数筛法.

参考代码

#include <stdio.h>#include <string.h>const int maxn = 6000000;bool IsPrime[maxn+10];int prime[maxn/2],tot = 0;int main(){    memset(IsPrime,true,sizeof(IsPrime));    for (int i = 2;i <= maxn;i++){        if (IsPrime[i]) prime[tot++] = i;        for (int j = 0;j < tot;j++){            if (i*prime[j] > maxn)  break;            IsPrime[i*prime[j]] = false;            if (i%prime[j] == 0) break;        }    }    for (int i = 0;i < tot;i++)        printf("%d\n",prime[i]);    return 0;}
0 0