[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.



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


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


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

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


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

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