zoukankan      html  css  js  c++  java
  • 【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;
    }
  • 相关阅读:
    PyQt4信号与槽
    Amazon Redshift数据库
    NoSQL数据库的认识
    如何划分子网
    VPC见解
    Linux之添加交换分区
    MySQL基础之 标准模式通配符
    MySQL基础之 LIKE操作符
    MySQL基础之 AND和OR运算符
    MySQL基础之 索引
  • 原文地址:https://www.cnblogs.com/tuty/p/1836745.html
Copyright © 2011-2022 走看看