Advertisement
danixan

Sessió 6: Estadística d'una seqüència d'enters amb esborrat

Apr 5th, 2015
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 KB | None | 0 0
  1. #include <queue>
  2. #include "utils.PRO2"
  3.  
  4. void ordenar(queue<int>& n, int& min, int& max) {
  5.     queue<int> s;
  6.     s = n;
  7.     n.pop();
  8.     for (int i = 0; i <= n.size(); ++i)  {
  9.         if (n.front() > max) max = n.front();
  10.         if (n.front() < min) min = n.front();
  11.         n.pop();
  12.     }
  13.     n = s;
  14. }
  15.  
  16. int main() {
  17.     queue <int> n;
  18.     int m = readint();
  19.     double mitjana = 0.0;
  20.     int num = 0;
  21.     while (m > -1002 and m < 1001) {
  22.         int min, max;
  23.         if (m == -1001) {
  24.             if (not n.empty()) {
  25.                 mitjana = mitjana - n.front();
  26.                 num = num - 1;
  27.                 n.pop();
  28.             }
  29.         }
  30.         else if (m != -1001) { 
  31.             num += 1;
  32.             mitjana += m;
  33.             n.push(m); 
  34.         }
  35.         if (not n.empty()) {
  36.             if (num == 1) min = max = n.front();
  37.             else ordenar(n, min, max);
  38.             cout << "min: " << min;
  39.             cout << " max: " << max;
  40.             cout << " mitjana: " << mitjana/num << endl;
  41.         }
  42.         else if (n.empty()) cout << "0" << endl;
  43.         m = readint();
  44.     }
  45.     cout << endl;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement