csansoon

*List & BinTre* X27494 Estadístiques d'una seqüència d'enter

Mar 22nd, 2019
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.01 KB | None | 0 0
  1. #include <iostream>
  2. #include <list>
  3. using namespace std;
  4.  
  5. int main() {
  6.     list<int> llista;
  7.     int codi, nombre;
  8.     while (cin >> codi >> nombre and codi!=0) {
  9.         if (codi == -1) {
  10.             list<int>::iterator it = llista.begin();
  11.             while (it != llista.end() and (*it)<nombre) ++it;   //Ordeno la lista de menor a mayor :)
  12.             llista.insert(it,nombre);                           //pero al parecer al jutge le parece "poco eficiente" >:v
  13.         }
  14.         else if (codi == -2) {
  15.                 list<int>::iterator it = llista.begin();
  16.                 while (it!=llista.end() and (*it)!=nombre) ++it;
  17.                 if (it != llista.end()) llista.erase(it);
  18.         }
  19.        double mitjana=0;
  20.        for (list<int>::iterator it = llista.begin(); it!=llista.end(); ++it) mitjana += (*it);
  21.        if (not llista.empty()) {
  22.            mitjana /= llista.size();
  23.            cout << *llista.begin() << " " << *(--llista.end())  << " " << mitjana << endl;
  24.        }
  25.        else cout << "0" << endl;
  26.     }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment