Uva 514 Rails(栈应用)

来源:互联网 发布:网络诈骗5000元 编辑:程序博客网 时间:2024/06/10 04:19

Uva 514 Rails

原题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=7&page=show_problem&problem=455



题意:  1-n编号的车从A驶入车站然后出站到B 问能否按照你输入的顺序输出  (进入中转站Station可以暂时停在里面 要满足后进先出)简单的栈应用

#include <stdio.h>int main(){    const int MAXN=10010;    int t[MAXN],n;    while(scanf("%d",&n)&&n!=0)    {        while(scanf("%d",&t[1]))        {            int stack[MAXN];            int p=0,i,a=1,b=1;            int c=1;            if(t[1]==0)break;            for(i=2; i<=n; i++)            {                scanf("%d",&t[i]);            }            while(b<=n)            {                if(a==t[b])                {                    a++;                    b++;                }                else if(p && stack[p]==t[b])                {                    p--;                    b++;                }                else if(a<=n) stack[++p]=a++;                else                {                    c=0;                    break;                }            }            printf("%s\n",c ? "Yes" : "No");        }        printf("\n");    }    return 0;}


1 0
原创粉丝点击