Advertisement
Tvor0zhok

Очередь STL

Mar 20th, 2021
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <cassert>
  3. #include <queue>
  4. using namespace std;
  5.  
  6. #define Type int
  7.  
  8. int count (queue <Type> q)
  9. {
  10. int res = 0;
  11.  
  12. Type n1 = q.front(); q.pop();
  13.  
  14. while(!q.empty())
  15. {
  16. Type n2 = q.front(); q.pop();
  17.  
  18. if (n1 == n2) ++res;
  19.  
  20. n1 = n2;
  21. }
  22.  
  23. return res;
  24. }
  25.  
  26. void del (queue <Type> &q, int n)
  27. {
  28. Type n1 = q.front(); q.pop(); q.push(n1);
  29.  
  30. for (int i = 1; i < n; ++i)
  31. {
  32. n1 = q.front(); q.pop();
  33.  
  34. Type n2 = q.back();
  35.  
  36. if (n1 != n2) q.push(n1);
  37. }
  38. }
  39.  
  40. void print (queue <Type> q)
  41. {
  42. while (!q.empty())
  43. {
  44. cout << q.front() << " ";
  45. q.pop();
  46. }
  47.  
  48. cout << '\n';
  49. }
  50.  
  51. int main()
  52. {
  53. int n; cout << "n = "; cin >> n;
  54.  
  55. queue <Type> q;
  56.  
  57. for (int i = 0; i < n; ++i)
  58. {
  59. Type a; cin >> a;
  60.  
  61. q.push(a);
  62. }
  63.  
  64. assert(q.size());
  65.  
  66. print(q);
  67.  
  68. cout << "Число пар: " << count(q) << '\n';
  69.  
  70. del(q, n);
  71. print(q);
  72. return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement