题解 | #火车进站#
火车进站
https://www.nowcoder.com/practice/97ba57c35e9f4749826dc3befaeae109
// 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;
}

查看3道真题和解析
海康威视公司福利 1407人发布