PAT_1051. Pop Sequence

来源:互联网 发布:动画人物制作软件 编辑:程序博客网 时间:2024/05/18 02:15
////  main.cpp//  PAT_1051. Pop Sequence////  Created by wjq on 17/5/14.//  Copyright © 2017年 wjq. All rights reserved.//#include <iostream>#include <stack>using namespace std;stack<int> s;int M,N,K,seq[1005];bool check(){    int num=1,target=0;    while(num!=N+1||!s.empty())    {        if((s.empty()==true||s.top()<seq[target])&&s.size()<M)        {            s.push(num);            num++;        }        else if(s.top()==seq[target])        {            s.pop();            target++;        }        else            return false;    }    return true;}int main(int argc, const char * argv[]){    cin>>M>>N>>K;    for(int i=0;i<K;i++)    {        for(int j=0;j<N;j++)            cin>>seq[j];        if(check()==true)            cout<<"YES"<<endl;        else            cout<<"NO"<<endl;        while(!s.empty())            s.pop();    }    return 0;}


栈的基本操作.

注意在

(s.empty()==true||s.top()<seq[target])

之中,两个条件位置不可以调换..一开始没把s.empty()条件写在前面.

导致了一个问题,如果此时栈是空的,我还用s.top()来进行大小判断,这会导致运行时错误.


0 0
原创粉丝点击