zl程序教程

您现在的位置是:首页 >  其它

当前栏目

P5736 【深基7.例2】质数筛

质数
2023-09-27 14:28:12 时间

题目传送门

#include <bits/stdc++.h>

using namespace std;

const int N = 110;
int a[N];

bool isPrime(int n) {
    if (n < 2) return false;
    for (int i = 2; i <= n / i; i++)
        if (n % i == 0)return false;
    return true;
}

int main() {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) cin >> a[i];
    for (int i = 1; i <= n; i++)
        if (isPrime(a[i]))
            cout << a[i] << " ";

    return 0;
}