题解 | 素数

素数

https://www.nowcoder.com/practice/7f4be54b37a04fdaa4ee545819151114

#include <iostream>
using namespace std;

const int N = 10010;
bool visit[N];
int main() {
    int n;
    while (cin >> n) { // 注意 while 处理多个 case
        int count=0;
        for(int i=2;i<n;i++){
            if(!visit[i]){
                count++;
                //此时i是素数
                if(i%10==1)printf("%d ",i);
                //进行筛
                for(int j=i+i;j<n;j+=i){
                    visit[j]=true;
                }
            }
        }
        if(!count)cout<<-1<<endl;
    }
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

10-17 13:54
上海大学 运营
雾凇岛:这还说什么了,冲了兄弟们
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务