判断一个顺序排列的栈的输出序列

利用C++自带的栈结构

源代码如下:

#include<cstdio>
#include<stack>
using namespace std;
const int MAX =1000 +10;
int target[MAX];
int n;
int main()
{
    while(scanf("%d",&n)==1)
    {    
    stack<int> s;
    int temp=1;
    int i=1,A=1,B=1;
    for(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
        {
            temp=0;
            break;
        }    
    }
        printf("%s
", temp? "yes" :"no"); 
    }
}

实验截图如下:

选择了远方,便只顾风雨兼程
原文地址:https://www.cnblogs.com/ly-rabbit-wust/p/5575163.html