[Mảng 1 Chiều Cơ Bản]. Bài 16. Liệt kê

Xem dạng PDF

Gửi bài giải

Điểm: 1,00 (OI)
Giới hạn thời gian: 1.0s
Giới hạn bộ nhớ: 256M
Input: stdin
Output: stdout

Tác giả:
Nguồn bài:
28Tech
Dạng bài
Ngôn ngữ cho phép
C, C#, C++, Java, Kotlin, Pascal, PyPy, Python, Scratch

Cho mảng số nguyên A[] gồm N phần tử, hãy đếm số lượng các phần tử thỏa mãn các yêu cầu sau

  1. Số lượng số nguyên tố trong dãy
  2. Số lượng số thuận nghịch trong dãy
  3. Số lượng số chính phương trong dãy
  4. Số lượng số có tổng chữ số của nó là số nguyên tố.

Đầu vào

Dòng đầu tiên là số nguyên dương N

Dòng thứ 2 gồm N số nguyên viết cách nhau một vài khoảng trắng


Giới hạn

1<=N<=100

0<=A[i]<=10000


Đầu ra

In ra 4 dòng số lượng số tương ứng với 4 yêu cầu trên


Ví dụ :

Input 01
6
4 1682 5972 6331 9872 3956
Output 01
0
1
1
4

Bình luận

Hãy đọc nội quy trước khi bình luận.



  • 0
    Nguyen0907  đã bình luận lúc 16, Tháng 8, 2025, 9:44

    include <bits/stdc++.h>

    using namespace std;
    
    #define fastIO() ios::sync_with_stdio(false); cin.tie(nullptr);
    #define ll long long
    
    bool isPrime(int n) {
        for(int i = 2; i <= sqrt(n); i++) {
            if (n % i == 0) return false;
        }
        return n >= 2;
    }
    
    bool soThuanNghich(int n) {
        int m = 0; int x = n;
        while(n) {
            m = m * 10 + (n % 10);
            n/=10;
        }
        return m == x;
    }
    
    bool chinhPhuong(int n) {
        int can = sqrt(n);
        if(1ll * can * can == n) {
            return true;
        }
        return false;
    }
    
    int sumPrimeDigits(int n) {
        int sum = 0;
        while(n) {
            sum += n%10;
            n/=10;
        }
        return sum;
    }
    int main() {
        fastIO();
    
        int n;
        cin >> n;
        int A[n];
        for (int i = 0; i < n; i++) {
            cin >> A[i];
        }
        int prime = 0,cp = 0,thuannghich = 0,sumprime = 0;
    
        for(int i = 0; i < n; i++) {
            if(isPrime(A[i])) {
                ++prime;
            }
            if(soThuanNghich(A[i])) {
                ++thuannghich;
            }
            if(chinhPhuong(A[i])) {
                ++cp;
            }
            if(isPrime(sumPrimeDigits(A[i]))) {
                ++sumprime;
            }
        }
    
        cout << prime << "\n" << thuannghich << "\n" << cp << "\n" << sumprime << endl;
        return 0;
    }
    

  • 0
    chau2010  đã bình luận lúc 25, Tháng 7, 2025, 7:42

    void solve() { int n,a=0,s=0; string s1, s2; cin>>n; vector<int> v(n); for(int&i:v)cin>>i; for(int i=0;i<n;i++) if(isPrime(v[i])) a++; cout << a << el; a = 0; for(int i=0;i<n;i++){ s1 = tostring(v[i]); s2 = s1; reverse(s1.begin(), s1.end()); if(s1 == s2) a++; } cout << a << el; a = 0; for(int i=0;i<n;i++) if(isPerfectSqr(v[i])) a++; cout << a << el; a = 0; for(int i=0;i<n;i++){ s = 0; for(char c : tostring(v[i])) s += c - '0'; if(isPrime(s)) a++; } cout << a; }


  • 0
    hngoc  đã bình luận lúc 4, Tháng 7, 2025, 14:19
    import math
    def nt(n):
        if n<=1:return False
        if n==2 or n==3:return True
        if n%2==0 or n%3==0:return False
        for i in range(5,math.floor(math.sqrt(n))+1,6):
            if n%i==0 or n%(i+2)==0:return False
        return True
    def tn(n):
        tmp=''
        for i in n:
            tmp=i+tmp
        return tmp==n
    def cp(n):
        a=int(math.sqrt(n))
        return a*a==n
    def t(n):
        s=0
        for i in n:
            s+=int(i)
        return nt(s)
    n=input()
    a=input().split()
    ans=[0,0,0,0]
    for x in a:
        if nt(int(x)):
            ans[0]+=1
        if tn(x):
            ans[1]+=1
        if cp(int(x)):
            ans[2]+=1
        if t(x):
            ans[3]+=1
    for x in ans:
        print(x)
    

  • -2
    hoanghaha  đã bình luận lúc 23, Tháng 2, 2025, 14:36

    include <bits/stdc++.h>

    using namespace std;

    define ll long long

    ll cnt=0,s=0,chinhphuong=0,thuannghich=0,ngto=0,tong=0,b; ll doixung(ll n) { ll r=0,o=n; while(n>0) { r=r10+(n%10); n/=10; } return r==o; } bool snt(ll n) { if(n<=1) return false; if(n<=3) return true; if(n%2==0||n%3==0) return false; for(ll i=5;ii<=n;i+=6) { if(n%i==0||n%(i+2)==0) return false; } return true; } ll scp(ll n) { ll sqrtN=sqrt(n); return sqrtN*sqrtN==n; } ll tongso(ll n) { ll s=0; while(n>0) { s+=n%10; n/=10; } return s; } int main() { iosbase::syncwith_stdio(false); cin.tie(NULL); ll n; cin>>n; vector<ll> a(n); for(ll i=0;i<n;i++) { cin>>a[i]; if(snt(a[i])) ngto++; if(doixung(a[i])) thuannghich++; if(scp(a[i])) chinhphuong++; if(snt(tongso(a[i]))) tong++; } cout<<ngto<<" "<<thuannghich<<" "<<chinhphuong<<" "<<tong<<"\n"; return 0; }


  • -1
    binh  đã bình luận lúc 6, Tháng 2, 2025, 3:01

    import java.util.Scanner; public class m1clkesnttncp { public static boolean nTo(int n) { if(n < 2 ) { return false; } for(int i = 2; i<=Math.sqrt(n); i++) { if(n % i == 0) { return false; } } return true; } public static boolean tNghich(int n) { int m = 0, tmp = n; while(n != 0) { m = m * 10 + n % 10; n /= 10; } if(tmp == m) { return true; } return false; } public static boolean cPhuong(int n) { int can = (int)Math.sqrt(n); if(can * can == n) { return true; } return false; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] a = new int[n]; for(int i = 0; i<n; i++) { a[i] = sc.nextInt(); } int count = 0, counttn = 0, countcp = 0, countt = 0; for(int i = 0; i<n; i++) { if(nTo(a[i])) { ++count; } if(tNghich(a[i])) { ++counttn; } if(cPhuong(a[i])) { ++countcp; } } for(int i = 0; i<n; i++) { int b = 0, j = a[i]; while(j > 0) { b += j % 10; j /= 10; } if(nTo(b)) { ++countt; } } System.out.println(count); System.out.println(counttn); System.out.println(countcp); System.out.println(countt); } }


  • 2
    VDev  đã bình luận lúc 19, Tháng 1, 2025, 9:09

    FULL AC

    #include <bits/stdc++.h>
    #include <iomanip>
    #include <cmath>
    #include <climits>
    #define ll long long
    using namespace std;
    ll a[10000011];
    ll cnt = 0, s = 0, s2 = 0, thuannghich = 0, nguyento = 0, chinhphuong = 0, j, b;
    bool nt(ll n){
        if(n < 2){
            return false;
        }else
        for(ll i = 2; i <= sqrt(n); i++){
            if(n % i == 0){
                return false;
            }
        }
        return true;
    }
    int main(){
        ll n;
        cin >> n;
        for(ll i = 0; i < n; i++){
            cin >> a[i];
        }
        for(ll i = 0; i < n; i++){
                if(nt(a[i])){
                    cnt++;
                }
        }
        for(ll i = 0; i < n; i++){
            ll m = a[i], h = a[i], s2 = 0;
            while(h != 0){
                s = h % 10;
                s2 = (s2 * 10) + s;
                h /= 10;
            }
            if(s2 == m){
                thuannghich++;
            }
        }
        for(ll i = 0; i < n; i++){
            ll can = sqrt(a[i]);
            if(can * can == a[i]){
                chinhphuong++;
            }
        }
        for(ll i = 0; i < n; i++){
                b = 0;
                j = a[i];
                while(j > 0){
                    b += j % 10;
                    j /= 10;
                }
                if(nt(b)){
                    nguyento++;
                }
        }
        cout << cnt << endl;
        cout << thuannghich << endl;
        cout << chinhphuong << endl;
        cout << nguyento << endl;
        return 0;
    }
    

  • 2
    khanhhduy_1305  đã bình luận lúc 17, Tháng 9, 2024, 2:31

    include<bits/stdc++.h>

    using namespace std; int nguyento(int n) { if(n<2) return 0; for(int i=2;i<n;i++) if(n%i==0) return 0; return 1; } int tong(int n) { int s=0; while(n!=0) { s+=n%10; n/=10; } return s; } int thuannghich(int n) { int m = 0, tmp = n; while(n != 0){ m = m * 10 + n % 10; n /= 10; } if(tmp == m){ return 1; } return 0; } int main() { int nt=0,tn=0,cp=0,tong1=0; int n; cin>>n; int a[n+9]; for(int i=0;i<n;i++) { cin>>a[i]; if (nguyento(a[i])) ++nt; if (thuannghich(a[i])) ++tn; if (sqrt(a[i])==trunc(sqrt(a[i]))) ++cp; if(nguyento(tong(a[i]))) ++tong1; } cout<<nt<


  • 0
    NhatTien2912  đã bình luận lúc 14, Tháng 8, 2024, 18:56

    include <iostream>

    include <vector>

    include <cmath>

    include <iomanip>

    include <set>

    include <map>

    include <algorithm>

    bool cp(int n) { int x = sqrt(n); if (x * x == n) return 1; else return 0; } bool isprime(int n) { if (n < 2) return 0; for (int i = 2; i <=sqrt(n); i++) { if (n % i == 0) return 0; } return 1; } bool palin(int n) { int org = n; // loi thuong gap vi n da bi thay doi int x = 0; while (n) { int tmp = 0; tmp = n % 10; x = x * 10 + tmp; n /= 10; } if (x== org) return 1; else return 0; } bool sip(int n) {

    int sum = 0;
    while (n) {
        int tmp = 0; 
        tmp = n % 10;
        sum += tmp;
        n /= 10;
    }
    if (isprime(sum)) return 1;
    else return 0;
    

    } using namespace std;
    int main(){ int cnt1 = 0, cnt2 = 0, cnt3 = 0, cnt4 = 0; int n; cin >> n; vector<int> a(n); for (int& x : a) cin >> x; for (int i = 0; i < n; i++) { if (isprime(a[i])) cnt1++; else if (palin(a[i])) cnt2++; else if (cp(a[i])) cnt3++; else if (sip(a[i])) cnt4++; } cout << cnt1 << endl; cout << cnt2 << endl; cout << cnt3 << endl; cout << cnt4 ; return 0; }

    sao bi wrong ans nhieu test case qua :<


  • -12
    hoangphuong  đã bình luận lúc 17, Tháng 5, 2024, 13:32

    Bình luận này đã bị ẩn vì có quá nhiều phản ứng tiêu cực. Nhấn để xem.