• 【POJ1363】Rails


    来源:Rails

    推导:建立数据结构队列A和栈B,题意即从A和B中向铁轨B按指定顺序输出车厢。输出车厢x有四种情况:

      1、B空      x在A中  将A中x前面的车厢全部入B,x出队列,返回true

      2、x>B顶  x在A中  同上

      3、x=B顶  x在B顶  B顶出栈,返回true

      4、其它情况   失败      直接返回false

    设计:

      void InitA(int n)  初始化A

      void InitB()    初始化B

      int Main()       必须的

      bool Pop(int x)   输出车厢x,参见上面的推导。

      bool Reorg(int n)     测试一行。返回false表示结束当前案例,重新读入车厢数量n

    代码:

    #include <iostream>
    #include
    <stack>
    #include
    <queue>
    using namespace std;

    queue
    <int> A;
    stack
    <int> B;

    void InitA(int n)
    {
    while(!A.empty())
    A.pop();
    for( int i=1;i<=n;i++ )
    A.push(i);
    }

    void InitB()
    {
    while(!B.empty())
    B.pop();
    }

    bool Pop(int x)
    {
    if( B.empty() || x>B.top() )
    {
    while(!A.empty())
    {
    if( A.front() != x )
    {
    B.push(A.front());
    A.pop();
    }
    else
    {
    A.pop();
    return true;
    }
    }
    }
    else if(x==B.top())
    {
    B.pop();
    return true;
    }
    return false;
    }

    bool Reorg(int n)
    {
    int x;
    bool result = true;
    for( int i=1;i<=n;i++ )
    {
    cin
    >> x;
    if( x==0 )
    {
    cout
    << endl;
    return false;
    }
    else
    {
    if( !Pop(x) )
    {
    result
    = false;
    }
    }

    }
    cout
    << (result?"Yes":"No") << endl;
    return true;
    }

    int main()
    {
    int n;
    while(cin>>n&&n!=0)
    {
    do
    {
    InitA(n);
    InitB();
    }
    while(Reorg(n));

    }
    return 1;
    }
  • 相关阅读:
    [HNOI 2015]菜肴制作
    [HNOI 2015]落忆枫音
    [NOIp 2009]靶形数独
    [HNOI 2010]Bounce 弹飞绵羊
    [CTSC 1999]拯救大兵瑞恩&[网络流24题]孤岛营救问题
    [SDOI 2008]Cave 洞穴勘测
    pandas 5 str 参考:https://mp.weixin.qq.com/s/Pwz9iwmQ_YQxUgWTVje9DQ
    比较工具
    当小内存遇上大量数据,你该怎么解决这个问题?
    python高性能编程 读书笔记
  • 原文地址:https://www.cnblogs.com/tuty/p/1836745.html
Copyright © 2020-2023  润新知