题解 | #火车进站#
火车进站
https://www.nowcoder.com/practice/97ba57c35e9f4749826dc3befaeae109
// HJ77-2 火车进站.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
int a[15];
int n;
int c[15];
bool check(int b[])
{
stack<int>st;
int j = 0;
for (int i = 0; i < n; i++)
{
st.push(a[i]);
while (!st.empty() && st.top() == b[j])
{
st.pop();
j++;
}
}
return st.empty();
}
int main()
{
while (cin >> n)
{
for (int i = 0; i < n; i++)
{
cin >> a[i];
c[i] = a[i];
}
sort(c,c+n);
do
{
if (check(c))
{
for (int i = 0; i < n; i++)
{
cout << c[i] << " ";
}
cout << endl;
}
} while (next_permutation(c,c+n));
}
return 0;
}

查看15道真题和解析