JosepRivaille

P63414: Comptant freqüències

Apr 29th, 2015
2,244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.48 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6.  
  7. int main() {
  8.     int n;
  9.     cin >> n;
  10.     if (n > 0) {
  11.         vector<int> v(n);
  12.         for (int i = 0; i < n; ++i) {
  13.             cin >> v[i];
  14.         }
  15.         sort(v.begin(),v.end());
  16.         int comp = v[0];
  17.         int count = 0;
  18.         for (int i = 0; i < n; ++i) {
  19.             if (v[i] == comp) ++count;
  20.             else {
  21.                 cout << comp << " : " << count << endl;
  22.                 count = 1;
  23.                 comp = v[i];
  24.             }
  25.         }
  26.         cout << comp << " : " << count << endl;
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment