文章

7

粉丝

0

获赞

18

访问

866

头像
素数 题解:
P1375 北京航空航天大学机试题
发布于2026年2月21日 08:57
阅读数 11

#include <bits/stdc++.h>
using namespace std;

bool is_res(int a){
    bool flag = 1;
    for(int i = 2; i <= sqrt(a); i++){  // 注意是 <=
        if(a % i == 0){
            flag = 0;
            break;
        }
    }
    if(flag){
        if(a % 10 == 1)
            return true;
        else
            return false;
    }
    else
        return false;
}

int main() {
    int n;
    while(cin >> n){
        int cnt = 0;
        int ans[10000];
        for(int i = 2; i < n; i++){
            if(is_res(i))
                ans[++cnt] = i;
        }
        if(cnt == 0)
            cout << -1 << endl;
        else{
            for(int i = 1; i < cnt; i++){
                cout << ans[i] << " ";
            }
            cout << ans[cnt] << endl;
        }
    }
    return 0;
}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发