[Mảng 1 Chiều Cơ Bản]. Bài 4. Lớn hơn, nhỏ hơn

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ử và số nguyên X , hãy đếm xem trong mảng có bao nhiêu số lớn hơn X và bao nhiêu số nhỏ hơn X.


Đầ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

Dòng thứ 3 là số nguyên X


Giới hạn

1<=N<=1000

-10^3<=A[i]<=10^3


Đầu ra

Dòng 1 in ra các số nhỏ hớn X, dòng 2 in ra các số lớn hơn X


Ví dụ :

Input 01
5
-798 183 434 850 555 
135
Output 01
1
4

Comments

Please read the guidelines before commenting.



  • 0
    L8_DuongThanhKhiem  commented on Nov. 22, 2025, 4:00 a.m.

    FULL AC:

    #include <bits/stdc++.h>
    using namespace std;
    const int oo = 1e9 + 7;
    short n;
    int demnho = 0,demlon = 0, x;
    vector <int> a;
    int main(){
        cin >> n;
        a.resize(n);
        for (int i = 0;i < n;i++)cin >> a[i];
        cin >> x;
        for (int i = 0;i < n;i++){
            if (a[i] < x)demnho++;
            if (a[i] > x)demlon++;
        }
        cout << demnho << endl;
        cout << demlon;
        return 0;
    }
    

  • 0
    linh_2008123  commented on Oct. 21, 2025, 2:07 a.m.

    include<bits/stdc++.h>

    using namespace std; int main(){ iosbase::syncwith_stdio(false); cin.tie(NULL); int n;cin>>n; int a[n]; int great=0,small=0; for(int i=0;i<n;i++){ cin>>a[i]; } int x;cin>>x; for(int i =0;i<n;i++){ if(x<a[i]){ ++great; } else if(a[i]<x){ ++small; } } cout<<small<<"\n"; cout<<great<<"\n"; return 0; }


  • 0
    naipret  commented on Sept. 15, 2025, 2:38 p.m.
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    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 threshold;
      cin >> threshold;
    
      int greater_than_threshold = 0, less_than_threshold = 0;
      for (int ele : vec) {
        if (ele > threshold) {
          greater_than_threshold++;
        }
        if (ele < threshold) {
          less_than_threshold++;
        }
      }
    
      cout << less_than_threshold << '\n';
      cout << greater_than_threshold;
    }
    

  • 0
    quoctu  commented on Aug. 9, 2025, 2:39 a.m.

    include <bits/stdc++.h>

    include <algorithm>

    include <cmath>

    include <math.h>

    using namespace std;

    using ll = long long;

    int main() { ll n; cin >> n; int a[n]; for ( int i = 0; i < n; i++){ cin >> a[i]; } ll x; cin >> x; int sn = 0; int sl = 0; for ( int i = 0; i < n; i++){ if (a[i] < x ){ sn++; } else if ( a[i] > x){ sl++; } } cout << sn << endl; cout << sl; return 0; }


  • -1
    Pqvinh07072015555  commented on May 7, 2025, 3:47 p.m. edited

    code python: enter image description here


  • -11
    thanhnay29  commented on Feb. 13, 2025, 7:38 a.m.

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


  • 0
    nguyen_luong_an  commented on Dec. 18, 2024, 2:27 p.m.

    <3


  • -3
    mango_K  commented on Dec. 3, 2024, 3:12 p.m.

    bài này sai ở đâu ko mn,sao đúng đc 23 test vậy,

    include <bits/stdc++.h>

    using namespace std; int main() { int d1=0,d2=0; int n,x; cin>>n; int a[n]; for(int i=0;i<n;i++) cin >> a[i]; cin>>x; for(int i=0;i<n;i++){ if (a[i]<x) d1++; else if(a[i>x]) d2++; } cout<<d1<


    • 2
      bengokyeuanh99  commented on April 18, 2025, 6:17 a.m.

      Lỗi logic cực nặng:

      else if(a[i > x]) d2++;

      i > x là biểu thức so sánh, trả về true (1) hoặc false (0)

      a[i > x] tương đương a[1] hoặc a[0] tức là bạn đang truy cập sai phần tử mảng, không phải so sánh a[i] > x

      In ra kết quả bị thiếu hoặc lỗi cú pháp:

      cout<<d1<

      Thiếu phần còn lại (<< và d2) compiler báo lỗi syntax, hoặc in không đủ kết quả sai output lỗi này dễ gặp khi viết nhanh, không check kỹ output

      Dùng mảng kiểu C:

      int a[n];

      Với n <= 1000 thì có thể chạy được, nhưng về mặt kỹ thuật: đây là VLA (Variable Length Array) không chuẩn C++, một số compiler không hỗ trợ

      dùng vector<int> a(n); an toàn, sạch, dễ bảo trì


  • -4
    ngocshintran  commented on Nov. 6, 2024, 10:49 a.m.

    CODE

    #include <bits/stdc++.h>
    
    using namespace std;
    
    int main() {
      int n,x;
      cin >> n;
      vector<int> a(n);
      for (int &x : a) cin >> x;
      cin >> x;
      int min = 0, max = 0;
      for (int it : a) {
        if (it > x) max++;
        else if (it < x) min++;
      }
      cout << min << endl << max;
      return 0;
    }
    

  • -2
    Coder  commented on Sept. 26, 2024, 1:31 a.m.
    #include <iostream>

    using namespace std;

    int main() { int arr[10001]; int n; cin >> n;

    for(int i = 0; i < n; i++) {
        cin >> arr[i];
    }
    
    int x; 
    cin >> x;
    
    int min = 0; 
    int max = 0; 
    
    for(int i = 0; i < n; i++) {
        if(arr[i] < x) {
            min++;
        } else if(arr[i] > x) {
            max++;
        }
    }
    
    cout << min << endl;  
    cout << max;
    
    return 0;
    

    }


  • -8
    duy210  commented on Aug. 26, 2024, 5:37 p.m.

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


  • -11
    theguy777_jaboi  commented on July 26, 2024, 8:33 a.m.

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


  • -7
    dangkhoa2836  commented on July 25, 2024, 3:06 p.m.

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


  • -13
    bynk2311  commented on June 9, 2024, 10:11 a.m.

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