Advertisement
JosepRivaille

X27494: Estadístiques d'una seqüència d'enters amb esborrat

Oct 21st, 2015
904
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.28 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <list>
  4. using namespace std;
  5.  
  6.  
  7. struct Pair {
  8.   int val;
  9.   int freq;
  10. };
  11.  
  12. //Afegir element a la llista
  13. void plus_list(list<Pair>& l, int el, int& max, int &min, double& ave) {
  14.   list<Pair>::iterator it= l.begin();
  15.   bool found = false;
  16.   while (it != l.end() && !found) {
  17.     Pair p = *it;
  18.     if (p.val == el) found = true;
  19.     else ++it;
  20.   }
  21.   if (!found) {
  22.     Pair p;
  23.     p.val = el;
  24.     p.freq = 1;
  25.     l.insert(it, p);
  26.     if (max < 0 && min < 0) max = min = el;
  27.     else if (el > max) max = el;
  28.     else if (el < min) min = el;
  29.   }
  30.   else {
  31.     Pair p = *it;
  32.     ++p.freq;
  33.     *it = p;
  34.   }
  35.   //Càlcul AVERAGE
  36.   ave = 0;
  37.   int count = 0;
  38.   for (it = l.begin(); it != l.end(); ++it) {
  39.     Pair p = *it;
  40.     ave += p.val * p.freq;
  41.     count += p.freq;
  42.   }
  43.   ave /= count;
  44. }
  45.  
  46.  
  47. //Eliminar element de la llista
  48. void minu_list(list<Pair>& l, int el, int& max, int &min, double& ave) {
  49.   if (!l.empty()) {
  50.     list<Pair>::iterator it = l.begin();
  51.     bool found = false;
  52.     //Eliminar
  53.     while (!found && it != l.end()) {
  54.       Pair p = *it;
  55.       if (p.val == el) {
  56.     if (p.freq == 1) it = l.erase(it);
  57.     else {
  58.       --p.freq;
  59.       *it = p;
  60.       ++it;
  61.     }
  62.       }
  63.       else ++it;
  64.     }
  65.     //Max i Min
  66.     if (l.empty()) max = min = 0;
  67.     else {
  68.       it = l.begin();
  69.       Pair p = *it;
  70.       max = min = p.val;
  71.       for (it = l.begin(); it != l.end(); ++it) {
  72.     Pair aux = *it;
  73.     if (aux.val > max) max = aux.val;
  74.     else if (aux.val < min) min = aux.val;
  75.       }
  76.     }
  77.     //Càlcul AVERAGE
  78.     ave = 0;
  79.     int count = 0;
  80.     for (it = l.begin(); it != l.end(); ++it) {
  81.       Pair p = *it;
  82.       ave += p.val * p.freq;
  83.       count += p.freq;
  84.     }
  85.     ave /= count;
  86.   }
  87.   if (l.empty()) max = min = -1;
  88. }
  89.  
  90.  
  91. int main() {
  92.   list<Pair> l;
  93.   int op, el;
  94.   bool finish = false;
  95.   int max, min;
  96.   max = min = -1;
  97.   double ave = 0;
  98.   while (!finish && cin >> op >> el) {
  99.     if (op == -1) plus_list(l, el, max, min, ave);
  100.     else if (op == -2) minu_list(l, el, max, min, ave);
  101.     else if (op == 0 && el == 0) finish = true;
  102.     if (!finish) {
  103.       if (max == -1 && min == -1) cout << "0" << endl;
  104.       else cout << min << " " << max << " " << ave <<  endl;
  105.     }
  106.   }
  107. }
  108.  
  109. //JosepRivaille
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement