我又双叒叕来提问了!C++实现

题目描述
输入 n(n\le100)n(n≤100) 个不大于 100000 的整数。要求全部储存在数组中,去除掉不是质数的数字,依次输出剩余的质数。

输入格式

输出格式

输入输出样例
输入 #1
5
3 4 5 6 7
输出 #1
3 5 7

#include <algorithm>
#include <math.h>
#include <iostream>
#include <iterator>
#include <vector>

using namespace std;

// 不是质数返回true
bool notPrime(int n)
{
    if (n == 1)
        return true;
    for (int i = 2; i < sqrt(n) + 1; i++)
        if (n % i == 0)
            return true;
    return false;
}

int main()
{
    vector<int> v, res;
    int n, t;
    cin >> n;
    int i = 0;
    for (i; i < n; i++)
    {
        cin >> t;
        v.push_back(t);
    }
    // 去除非质数
    remove_copy_if(v.begin(), v.end(), back_inserter(res), notPrime);
    // 输出
    copy(res.begin(), res.end(), ostream_iterator<int>(cout, " "));
    return 0;
}