zoukankan      html  css  js  c++  java
  • C++练习 | 铁轨问题

    #include <iostream>
    #include <cmath>
    #include <cstring>
    #include <string>
    #include <iomanip>
    #include <algorithm>
    #include <stack>
    using namespace std;
    int a[10007];
    int main()
    {
        int n,t,num;
        stack<int> s1,s2;
        while(cin>>n)
        {
            num=0;
            
            for(int i=1;i<=n;i++)
            {
                cin>>t;
                a[i]=t;
                s1.push(n-i+1);
            }//输入
            while(!s1.empty()||!s2.empty())
            {
                num++;
                if(!s2.empty())
                {//s2不空时
                    if(s2.top()==a[num])
                    {
                        s2.pop();
                        continue;
                    }
                }
                if(!s1.empty())
                {
                    while(s1.top()!=a[num]&&!s1.empty())
                    {
                        t=s1.top();
                        s2.push(t);
                        s1.pop();
                    }
                    if(s1.top()==a[num])
                    {
                        s1.pop();
                    }
                    else
                    {
                        cout<<"No"<<endl;
                        break;
                    }
                }
                else
                {
                    cout<<"No"<<endl;
                    break;
                }
            }
            if(num==n)
            {
                cout<<"Yes"<<endl;
            }
            while(!s1.empty())
            {
                s1.pop();
            }
            while(!s2.empty())
            {
                s2.pop();
            }
        }
        return 0;
    }

    属于STL中stack的使用

    注意对于栈是否空的判断避免溢出

  • 相关阅读:
    密码学基础(三)
    密码学基础(二)
    密码学基础(一)
    Lambda代数
    恢复系统
    11.22面试例题
    js中级复习
    11.12
    11.13
    定时器
  • 原文地址:https://www.cnblogs.com/tsj816523/p/11037756.html
Copyright © 2011-2022 走看看