csansoon

P7.13 X68753 Moda

Nov 14th, 2018
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.47 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5.  
  6. void moda(int n, const vector<int>& p) {
  7.         int comp = 0;
  8.         int major;
  9.         int moda_aux;
  10.         for (int i = 0; i < n; ++i) {
  11.                 int rep = 0;
  12.                 for (int j = 0; j < n; ++j) {
  13.                         if (p[i] == p[j]) {
  14.                                 ++rep;
  15.                                 if (rep > comp) {
  16.                                         moda_aux = p[i];
  17.                                         major = p[i];
  18.                                         comp = rep;
  19.                                 }
  20.                                 else if (rep == comp) {
  21.                                         if (p[i] > moda_aux) {
  22.                                                 moda_aux = p[i];
  23.                                                 major = p[i];
  24.                                                 comp = rep;
  25.                                         }
  26.                                 }
  27.                         }
  28.                 }
  29.         }
  30.         cout << major << endl;
  31. }
  32.  
  33.  
  34. //Pre: Llegeix diverses seqüències de nombres
  35. //Post: Escriu la moda de cada seq.
  36. int main() {
  37.         int n;
  38.         while (cin >> n) {
  39.                 vector<int> p(n);
  40.                 for (int i = 0; i < n; ++i) {
  41.                         cin >> p[i];
  42.                 }
  43.                 moda(n, p);
  44.         }
  45. }
  46.  
  47. // (c) Carlos Sansón (Best pro1 delegate ever for sure) @csansoon
Advertisement
Add Comment
Please, Sign In to add comment