Advertisement
Tvor0zhok

Стек STL

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