很简单的模拟题,也是学到栈时很经典的填空题。按照做题时的思路一步步写下来就好了。在uDebug时发现最后还要多输出一个空行,算是易错点吧。
题目链接:UVa 514
AC代码:
#include <iostream>
//#include <fstream>
#include <stack>
using namespace std;
const int maxn = 2000;
int main() {
int T, N, count = 0;
//ofstream out("C:\\Users\\Acer\\Desktop\\1.txt");
while (cin >> T && T) {
//if (out.is_open()) {
if (count++)
cout << endl;
//}
while (cin >> N && N) {
stack<int> s;
int train[maxn];
train[1] = N;
for (int i = 2; i <= T; i++)
cin >> train[i];
bool flag = false;
for (int i = 1,j=1; j < T + 1;) { //将入站与出站车厢序列同时比较
if (train[j] == i) {
i++;
j++;
continue;
}
else if (!s.empty() && s.top() == train[j]) {
j++;
s.pop();
}
else if (i <= T) {
s.push(i);
i++;
}
else {
flag = true;
break;
}
}
//if (out.is_open()) {
if (flag)
cout << "No" << endl;
else
cout << "Yes" << endl;
//}
}
}
//if (out.is_open()) {
cout << endl;
//}
return 0;
}