例题6.1 铁轨【算法入门经典】

一个很简单的模拟题吧,但是我都不是很懂,好菜啊。。。

#include<stdio.h>
#include<stack>
using namespace std;
#define N 10010
int n,target[N];

int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        stack<int>s;
        int a=1,b=1;
        int ok = 0;
        for(int i = 1; i <= n; i ++)
            scanf("%d",&target[i]);
        while(b <= n)
        {
            if(a == target[b])//两编号相等时,同时后移 
            {
                a++;
                b++;
            }
            else if(!s.empty()&&s.top() == target[b])//当栈不为空且 栈编号和目标序列相等时 
            {
                s.pop();//出栈 
                b++;//目标序列后移 
            }
            else if(a <= n)//入栈 
                s.push(a++);
            else
            {
                ok = 1;
                break;
            }
        }
        printf("%s
",ok?"No":"Yes");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/hellocheng/p/7832404.html