[Mảng 1 Chiều Cơ Bản]. Bài 9. Tần suất

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 liệt kê các giá trị xuất hiện trong mảng kèm theo tần suất tương ứng, mỗi giá trị chỉ liệt kê một lần theo thứ tự xuất hiện.


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

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


Đầu ra

In ra nhiều dòng, mỗi dòng gồm giá trị kèm theo tần suất tương ứng


Ví dụ :

Input 01
7
4 2 6 3 0 7 7
Output 01
4 1
2 1
6 1
3 1
0 1
7 2
Input 02
5
1 1 1 1 1
Output 02
1 5

Comments

Please read the guidelines before commenting.



  • 0
    GodNona  commented on Feb. 1, 2025, 4:43 a.m.

    include <bits/stdc++.h>

    using namespace std;

    int a[1003];

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

    int n; cin >> n;
    map<int,int> mp;
    
    for(int i = 0; i < n; i++)
    {
        cin >> a[i];
        mp[a[i]]++;
    }
    for(int i = 0; i < n; i++)
    {
        if(mp[a[i]] != 0)
        {
            cout << a[i] << " " << mp[a[i]] << "\n";
            mp[a[i]] = 0;
        }
    }
    
    
    
    return 0;
    

    }


  • -1
    fan_messi10  commented on Jan. 19, 2025, 4:01 p.m.

    ý tưởng: sử dụng mảng đánh dấu + mảng phụ Mảng phụ dùng để đưa các giá trị khác nhau vào sau đó duyệt rồi in ra theo thứ tự.


  • 0
    dangsp07  commented on Dec. 11, 2024, 8:26 a.m.

    include <bits/stdc++.h>

    using namespace std; int main() { int n; cin >> n; int arr[n];

    for (int i = 0; i < n; i++) {
        cin >> arr[i];
    }
    unordered_map&lt;int, int> ts;
    vector<int> od;
    for (int i = 0; i < n; i++) {
        if (ts[arr[i]] == 0) {
            od.push_back(arr[i]);
        }
        ts[arr[i]]++;
    }
    for(int i = 0; i < od.size(); i++) {
        cout << od[i] << " " << ts[od[i]] << endl;
    }
    return 0;
    

    }


  • 0
    khiem101701  commented on Dec. 2, 2024, 4:20 p.m.

    a = int(input()) b = list(map(int, input().split())) c = [0] * (10**3 + 1) for i in b: c[i] += 1 for i in range (len(b)): flag = True for j in range (0, i): if(b[j] == b[i]): flag = False break if(flag): print(b[i], c[b[i]])


  • 0
    ichingu  commented on Nov. 1, 2024, 1:04 p.m.


  • 0
    khangsigma  commented on Oct. 12, 2024, 10:15 a.m.

    include <iostream>

    define N 10005

    using namespace std; int a[N],b[N]={0}; int main() { int n;cin>>n; for (int i=1;i<=n;i++)cin>>a[i]; for (int i=1;i<=n;i++) if (b[i]==0) { int d=0; for (int j=1;j<=n;j++) if (a[i]==a[j]) { d++; b[j]=1; } cout <<a[i]<<" "<<d<


  • -1
    thinhphamminh274  commented on Oct. 12, 2024, 9:43 a.m.

    include <bits/stdc++.h>

    define N 1005

    using namespace std; int a[N]; int main() { int n;cin>>n; for (int i=1; i<=n; i++)cin>>a[i]; for (int i=1;i<=n;i++) {int kt = true; for (int j=1;j<i;j++) if (a[i]==a[j]) {kt = false; break;}

    if (kt) cout<<a[i]<<" ";} return 0; }


  • 1
    Coder  commented on Sept. 26, 2024, 1:22 p.m.

    mọi người tham khảo nhé!

    #include <iostream>

    using namespace std;

    int find(int n, int* arr, int value) { int tmp = 0; for(int i = 0; i < n; i++) { if(arr[i] == value) { tmp++; } } return tmp; }

    int main() {

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    
    int arr[100001];
    int n; 
    cin >> n;
    
    for(int i = 0; i < n; i++) {
        cin >> arr[i];
    }
    
    for(int i = 0; i < n; i++) {
        bool found = false;
        for(int j = 0; j < i; j++) {
            if(arr[i] == arr[j]) {
                found = true;
                break;
            }
        }
    
        if(!found) {
            cout << arr[i] << " " << find(n, arr, arr[i]) << endl;
        }
    
    }
    
    return 0;
    

    }