Bài 21. Đếm số lần xuất hiện của mỗi phần tử trong dãy số. Ví dụ:
XUATHIEN.INP |
XUATHIEN.OUT |
6 3 5 3 6 5 7 |
3 2 5 2 6 1 7 1 |
Code tham khảo:
#include <iostream>
using namespace std;
int main() {
freopen("XUATHIEN.INP","r",stdin);
freopen("XUATHIEN.OUT","w",stdout);
int n, a[1000], b[1000];
cin >> n;
for(int i=0; i<n; i++) {
cin >> a[i];
b[i] = 1;
}
for(int i=0; i<n; i++) {
int d = 1;
if(b[i]) {
b[i] = 0;
for (int j=i+1; j<n; j++)
if(a[j] == a[i]) {
d++;
b[j] = 0;
}
cout << a[i] << " " << d << endl;
}
}
return 0;
}