Advertisement
myloyo

algorithm 2

Jun 7th, 2023 (edited)
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. class Sound {
  10.     int high, time;
  11. public:
  12.     Sound() : high(0), time(0) {}
  13.     Sound(int h, int t) : high(h), time(t) {}
  14.     int getHigh() {
  15.         return this->high;
  16.     }
  17.     int getTime() {
  18.         return this->time;
  19.     }
  20.     void print_sound() {
  21.         cout << "Note:" << this->high << " " <<" Duration:" << this->time << " sec" << endl;
  22.     }
  23. };
  24.  
  25. bool comporator(Sound left, Sound right) {
  26.     return (left.getHigh() < right.getHigh());
  27. }
  28.  
  29. int main() {
  30.     vector <Sound> melody;
  31.     int n;
  32.     int h, t;
  33.     cin >> n;
  34.     for (int i = 0; i < n; i++) {
  35.         cin >> h >> t;
  36.         melody.push_back({ h, t });
  37.     }
  38.     cin >> h >> t;
  39.     melody.push_back({ h, t });
  40.     sort(melody.begin(), melody.end(), comporator);
  41.     auto it = melody.begin();
  42.     while (it != melody.end()) {
  43.         it->print_sound();
  44.         it++;
  45.     }
  46.     return 0;
  47. }
  48.  
  49. 4
  50. 62 3
  51. 60 1
  52. 88 4
  53. 65 4
  54. 77 9
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement