首页 > 其他 > 详细

北航机试 素数 Easy

时间:2020-03-04 12:21:34      阅读:83      评论:0      收藏:0      [点我收藏+]

基本思想:

直接打表;

 

关键点:

无;

 

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<map>
#include<set>
using namespace std;

const int maxn = 11000;
int m, n;
bool num[maxn];

void init() {
    fill(num, num + maxn, true);
    num[0] = num[1] = false;
    for (int i = 2; i < maxn; i++) {
        if (num[i]) {
            //如果是素数;
            for (int j = i+i; j < maxn; j+=i) {
                num[j] = false;
            }
        }
    }
}

int main() {
    init();
    while (cin >> n) {
        bool flag = true;
        for (int i = 1; i < n; i++) {
            if(num[i]&&i%10==1)
                if (flag) {
                    cout << i;
                    flag = false;
                }
                else {
                    cout << " " << i;
                }
        }
        cout << endl;
    }
    return 0;
}

 

北航机试 素数 Easy

原文:https://www.cnblogs.com/songlinxuan/p/12408393.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!