Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cassert>
- #include <stack>
- using namespace std;
- #define Type int
- int count (stack <Type> st)
- {
- int res = 0;
- Type n1 = st.top(); st.pop();
- while(!st.empty())
- {
- Type n2 = st.top();
- if (n1 == n2) ++res;
- n1 = n2;
- st.pop();
- }
- return res;
- }
- void del (stack <Type> &st)
- {
- if (st.size() == 1) return;
- Type n1 = st.top();
- st.pop();
- Type n2 = st.top();
- del(st);
- if (n1 != n2) st.push(n1);
- }
- void print (stack <Type> st)
- {
- while(!st.empty())
- {
- cout << st.top() << " ";
- st.pop();
- }
- cout << '\n';
- }
- int main()
- {
- stack <Type> st;
- int n;
- cout << "n = "; cin >> n;
- for (int i = 0; i < n; ++i)
- {
- Type a; cin >> a;
- st.push(a);
- }
- assert(st.size());
- print(st);
- cout << "Число пар: " << count(st) << '\n';
- del(st);
- print(st);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement