您的位置:首页 > 大数据 > 人工智能

UVA 514 Rails(栈)

2015-07-28 09:54 447 查看
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=7&page=show_problem&problem=455

题       意:给你一个组数,问将1到n一次入栈后,能否按此顺序输出。

思       路:定义一个栈来维护就行了。

代码如下:

#include <iostream>
using namespace std;
#include <string.h>
#include <stdio.h>
#include <stack>
#include <algorithm>
int vis[1200];
int main()
{
int n;
while( scanf ( "%d", &n ) != EOF )
{
stack<int>st;
if( n == 0 ) break;
while( 1 )
{
for( int i = 1; i <= n; i ++ )
{
scanf ( "%d", &vis[i] );
if(vis[1]==0) break;
}
if( vis[1] == 0 ) break;
int a = 1, b = 1, ans = 1;
while( b <= n )
{
if( a == vis[b] )
{
a++;
b++;
}
else if( !st.empty() && st.top() == vis[b] )
{
st.pop();
b++;
}
else if( a <= n ) st.push(a++);
else
{
ans=0;
break;
}
}
if( ans == 1 ) printf("Yes\n");
else printf("No\n");
}
printf("\n");
}
return 0;
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  algorithm