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

View as PDF

Submit solution

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

Author:
Problem source:
28Tech
Problem type
Allowed languages
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

Comments

Please read the guidelines before commenting.



  • -1
    kietne  commented on Dec. 13, 2025, 2:10 a.m.

    DÀI CHỨ KO KHÓ NHA AE

    include <bits/stdc++.h>

    using namespace std; using ll = long long;

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

    bool thuannghich(int n){ string s = tostring(n); string s2 = s; reverse(s2.begin(), s2.end()); return s == s2; }

    bool chinh_phuong(int n){ int x = sqrt(n); return x * x == n; }

    bool tongnguyento(int n){ string s = tostring(n); int tong = 0; for(char c : s){ tong += c - '0'; } return nguyento(tong); }

    int main() { ios::syncwithstdio(false); cin.tie(nullptr);

    int nguyen_to1 = 0, tn = 0, cp = 0, tnt = 0;
    int n; cin >> n;
    for(int i = 0; i < n; i++){
        int x; cin >> x;
        if(nguyen_to(x)) nguyen_to1++;
        if(thuan_nghich(x)) tn++;
        if(chinh_phuong(x)) cp++;
        if(tong_nguyen_to(x)) tnt++;
    }
    cout << nguyen_to1 << "\n" << tn << "\n" << cp << "\n" << tnt;
    return 0;
    

    }


  • -1
    naipret  commented on Sept. 18, 2025, 6:55 a.m. edited
    #include <cmath>
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    bool IsPrime(int val) {
      if (val < 2) {
        return false;
      }
    
      int loop_limit = static_cast<int>(sqrt(val));
      for (int seq_val = 2; seq_val <= loop_limit; seq_val++) {
        if (val % seq_val == 0) {
          return false;
        }
      }
    
      return true;
    }
    
    bool IsPalindrome(int val) {
      int tmp = val, reversed = 0;
      while (tmp != 0) {
        reversed = reversed * 10 + (tmp % 10);
        tmp /= 10;
      }
    
      return val == reversed;
    }
    
    bool IsPerfectSquare(int val) {
      int sqrt_res = static_cast<int>(sqrt(val));
      return sqrt_res * sqrt_res == val;
    }
    
    int SumDigits(int val) {
      int sum = 0;
      while (val != 0) {
        sum += (val % 10);
        val /= 10;
      }
    
      return sum;
    }
    
    int main() {
      ios::sync_with_stdio(false);
      cin.tie(nullptr);
    
      int num;
      cin >> num;
    
      vector<int> vec(num);
      for (int &ele : vec) {
        cin >> ele;
      }
    
      int prime_cnt = 0, palindrome_cnt = 0, perfect_square_cnt = 0,
          sum_digits_if_prime_cnt = 0;
      for (int ele : vec) {
        if (IsPrime(ele)) {
          prime_cnt++;
        }
        if (IsPalindrome(ele)) {
          palindrome_cnt++;
        }
        if (IsPerfectSquare(ele)) {
          perfect_square_cnt++;
        }
        if (IsPrime(SumDigits(ele))) {
          sum_digits_if_prime_cnt++;
        }
      }
    
      cout << prime_cnt << '\n';
      cout << palindrome_cnt << '\n';
      cout << perfect_square_cnt << '\n';
      cout << sum_digits_if_prime_cnt;
    }
    

  • -1
    Nguyen0907  commented on Aug. 16, 2025, 9:44 a.m.

    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;
    }
    

  • -1
    chau2010  commented on July 25, 2025, 7:42 a.m.

    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; }


  • -1
    hngoc  commented on July 4, 2025, 2:19 p.m.
    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)
    

  • -3
    hoanghaha  commented on Feb. 23, 2025, 2:36 p.m.

    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; }


  • -2
    binh  commented on Feb. 6, 2025, 3:01 a.m.

    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); } }


  • 1
    VDev  commented on Jan. 19, 2025, 9:09 a.m.

    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;
    }
    

  • 1
    khanhhduy_1305  commented on Sept. 17, 2024, 2:31 a.m.

    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<


  • -1
    NhatTien2912  commented on Aug. 14, 2024, 6:56 p.m.

    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 :<


  • -13
    hoangphuong  commented on May 17, 2024, 1:32 p.m.

    This comment is hidden due to too much negative feedback. Show it anyway.