Advertisement
myname0

практика_алгоритмы_7

Jul 6th, 2015
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 0
  1. #include <fstream>
  2. #include <algorithm>
  3. #include <vector>
  4. #include <functional>
  5.  
  6. using namespace std;
  7.  
  8. ofstream out("output.txt");
  9.  
  10. class Sound
  11. {
  12. private:
  13.     double pitch;
  14.     double playingTime;
  15. public:
  16.     Sound(): pitch(0), playingTime(0)
  17.     {
  18.     }
  19.     void Set(double tmp, double temp)
  20.     {
  21.         pitch = tmp;
  22.         playingTime = temp;
  23.     }
  24.     bool comp(Sound &x)
  25.     {
  26.         if(x.pitch == this->pitch)
  27.             return x.playingTime < this->playingTime;
  28.         else return x.pitch > this->pitch;
  29.     }
  30.     void print()
  31.     {
  32.         out << pitch << " " << playingTime << endl;
  33.     }
  34. };
  35.  
  36.  
  37. int main()
  38. {
  39.     ifstream in("input.txt");
  40.     vector <Sound> melody;
  41.     Sound x;
  42.     int tmp, temp;
  43.     in >> tmp;
  44.     double n, m;
  45.     for(int i = 0; i < tmp; i++)
  46.     {
  47.         in >> n >> m;
  48.         x.Set(n, m);
  49.         melody.push_back(x);
  50.     }
  51.     in >> temp;
  52.     temp--;
  53.     in.close();
  54.     nth_element(melody.begin(), melody.begin() + temp, melody.end(), mem_fun_ref(&Sound::comp));
  55.     out << "N-element: ";
  56.     melody[temp].print();
  57.     out << "Sounds before N-element:" << endl;
  58.     for(int i = 0; i < temp; i++)
  59.         melody[i].print();
  60.     out <<"Sounds after N-element:" << endl;
  61.     for(int i = ++temp; i < tmp; i++)
  62.         melody[i].print();
  63.  
  64.     out.close();
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement