这个其实是比较简单的,只要你看懂了题目,利用栈的特性做就好,最开始我还是不懂为什么12345和54321都可以过,所以想请大神可以帮我解释一下;然后接下来是书上给得答案,这里面主要使用了stack函数库,一个创建函数,然后就是对栈顶元素的使用,push,top,pop三个函数
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <stack>
using namespace std;
int d[ 1005 ];
int u[ 1005 ];
int main()
{
int n;
while ( cin >> n && n ) {
while ( cin >> d[1] && d[1] ) {
for ( int i = 2 ; i <= n ; ++ i )
cin >> d[i];
stack<int> S;
int o = 1,k = 1;
while ( o <= n ) {
if ( !S.empty() && d[o] == S.top() ) {
S.pop();
++ o;
// printf("pop
");
}else if ( d[o] != k ) {
S.push(k);
if ( ++ k > n ) break;
// printf("push
");
}else {
++ o;
++ k;
// printf("move
");
}
}
if ( o > n )
cout << "Yes" << endl;
else cout << "No" << endl;
}
cout << endl;
}
return 0;
}