// 1. 使用全排列 next_permutation 进行操作 // 2. 使用全排列 next_permutation 前需要先进行 sort 排序操作 // 3. 使用 stack 栈 这个数据结构。 #include <algorithm> #include<iostream> #include <stack> #include <vector> using namespace std; int n; vector<int> v; vector<int> t; bool check(vector<int> & t){ bool bisOk = true; stack<int> st; int j = 0; for(int i = 0; i < n; ++i){ st.push(v[i]); while(!st.empty() && st.top() == t[j]){ st.pop(); j++; } } return (j == n); } int main(){ while(cin >> n){ int tmp; for(int i = 0; i < n; ++i){ cin >> tmp; v.push_back(tmp); t.push_back(tmp); } sort(t.begin(), t.end()); do{ if(check(t)){ for(auto it : t){ cout << it << " "; } cout << endl; } }while(next_permutation(t.begin(), t.end())); } return 0; }