Số nguyên tố

mail@pastecode.io avatar
unknown
c_cpp
2 months ago
634 B
0
Indexable
Never
#include <bits/stdc++.h>
using namespace std;

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

int main() {
    ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);

    int n; cin >> n;
    int a[n], maxPrime = INT_MIN;
    for (int i = 0; i < n; i++) {
        cin >> a[i];
        if (isPrime(a[i]) && a[i] > maxPrime) maxPrime = a[i];
    }
    cout << maxPrime << "\n";
    for (int i = 0; i < n; i++) {
        if (a[i] == maxPrime) cout << i+1 << " ";
    }

    return 0;
}
Leave a Comment