题解 | #火车进站#

火车进站

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;
}

全部评论

相关推荐

在考古的丘比特很冷艳:[牛泪我横向一周多自动挂了,面试答的还行,感觉挂的莫名其妙
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务