[Mảng 1 Chiều Cơ Bản]. Bài 55. Thay đổi nguyên tố

View as PDF

Submit solution

Points: 1.00 (partial)
Time limit: 1.0s
Memory limit: 256M
Input: stdin
Output: stdout

Problem source:
28Tech
Problem type
Allowed languages
C, C++, Java, Kotlin, Pascal, PyPy, Python, Scratch

Cho mảng A[] gồm N phần tử bạn hãy thay đổi tất cả các phần tử trong mảng là số nguyên tố thành số 28 và in ra mảng sau khi thay đổi.


Đầu vào

Dòng 1 là N : số phần tử trong mảng

Dòng 2 là N số viết cách nhau 1 dấu cách


Giới hạn

1≤N≤10^3

0≤A[i] ≤10^6


Đầu ra

In ra mảng sau khi thay đổi


Ví dụ :

Input 01
5
10 20 7 7 11
Output 01
10 20 28 28 28

Comments

Please read the guidelines before commenting.



  • 0
    zXCczxvzxv  commented on Jan. 24, 2026, 2:25 a.m.

    include <bits/stdc++.h>

    using namespace std;

    vector<bool> sieve(int n) { vector<bool> prime(n+1, true); prime[0] = prime[1] = false; for (int i = 2; i <= int(sqrt(n)); i++) { if (prime[i]) { for (int j = i*i; j <= n; j += i) { prime[j] = false; } } } return prime; }

    int main() { iosbase::syncwith_stdio(false); cin.tie(NULL);

    int n;
    cin >> n;
    vector<int> a(n);
    
    for (int i = 0; i < n; i++) cin >> a[i];
    
    int maxVal = *max_element(a.begin(), a.end());
    vector<bool> prime = sieve(maxVal);
    
    for (int i = 0; i < n; i++) {
        if (prime[a[i]]) {
            a[i] = 28;
        }
    }
    for (int i = 0; i < n; i++) {
        cout << a[i] << " ";
    }
    cout << "\n";
    

    }


  • 0
    kietne  commented on Dec. 29, 2025, 1:55 p.m.

    include <bits/stdc++.h>

    using namespace std; using ll = long long; using ull = unsigned long long; bool int(int n){ for (int i = 2; i <= sqrt(n);i++){ if(n%i==0) return false; } return n>1; } int main() { ios::syncwithstdio(false); cin.tie(nullptr); int n; cin >> n; while(n--){ int x; cin >> x; if(int(x)) cout << 28 << " "; else cout << x << " "; } return 0; }