• Problem R: 栈小游戏


    #include <iostream>
    #include <vector>
    #include <stack>
    using namespace std;
    class Operation
    {
    public :
        vector<int> data;
        stack<int> iStack;
        vector<char> opers;
        vector<int> result;
    
        void inputData()
        {
            int temp;
            while(cin>>temp)
            {
                if(temp==0)
                    break;
                else
                data.push_back(temp);
            }
        }
        void inputOpers()
        {
            char temp;
            while(cin>>temp)
            {
                if(temp=='E'||temp=='e')
                    break;
                else if(temp=='i'||temp=='I'||temp=='o'||temp=='O')
                    opers.push_back(temp);
            }
        }
        void showResult()
        {
            int j1=0, j2=0;
            cout<<"The pop sequence is:";
            for(int i=0; i<opers.size(); i++)
            {
                if(opers[i]=='i'||opers[i]=='I')
                {
                    if(j1<data.size())//当data里的数据数量满足不了入栈操作时停止入栈
                    iStack.push(data[j1++]);
                }
                else if(opers[i]=='o'||opers[i]=='O')
                {
                    if(!iStack.empty())//当堆栈为空时停止出栈
                    {
                        cout<<" "<<iStack.top();
                        result.push_back(iStack.top());//将出栈数据存到 result
                        iStack.pop();
                    }
                }
            }
                if(result.empty())//result 为空表明无数据出栈,输出empty
                cout<<"Empty.";
                cout<<endl;
        }
        void clearData()
        {
            data.clear();
            opers.clear();
            result.clear();
            while(!iStack.empty())// 堆栈无clear 用法。
                iStack.pop();
        }
    };
    int main()
    {
        Operation myOp;
        int cases;
        cin>>cases;
        for (int i = 0; i < cases; i++)
        {
            myOp.clearData();
            myOp.inputData();
            myOp.inputOpers();
            myOp.showResult();
        }
        return 0;
    }
    

      

    Problem R: 栈小游戏

    Time Limit: 1 Sec  Memory Limit: 128 MB
    Submit: 848  Solved: 545
    [Submit][Status][Web Board]

    Description

    小明想深入了解Stack(栈)的用法,所以他打算写一个类来模拟一下。但是他没有学过C++中强大的STL,所以他现在向你求助了。请帮他编写一个类Operation,包括4个属性:

    1. vector<int> data:存储用于仿真的数据。

    2. stack<int> iStack:栈。

    3. vector<char> opers:对栈的操作。

    4. vector<int> result:存储出栈的数据。

    还有4个方法:

    1. void inputData():读取数据。

    2. void inputOpers():读取操作。

    3. void showResult():显示出栈的数据序列。

    4. void clearData():清空所有的属性值。

    Input

    1N>0表示有N个测试用例。

    每个测试用例由两行组成:

    1. 1行是若干个int类型的数据,以“0”作为结束标识。注意:“0”不作为输入数据即Operation类中data成员的一个元素,只是用做结束标识。

    2. 2行是若干个英文字母,用'i''I'(字母I,不是数字1.)表示入栈操作,用‘o'或者'O’(字母O,不是数字0.)表示出栈操作,用'e'或者'E'表示输入结束。但是小明很不仔细,这些字符序列中可能包括其他字符。

    Output

    输出有N行,每个测试用例产生一行输出。

    每行的格式为:“The pop sequence is: ”,之后按照出栈顺序显示所有数据,这些数据两两之间用1个空格隔开,且最后没有空格。如果没有产生出栈数据,则显示“Empty.”

    Sample Input

    6

    1 2 3 4 5 6 7 8 -1 -2 -3 -4 -5 -6 -7 0

    i I I I o O o c c c e

    1 2 3 4 5 6 7 8 -1 -2 -3 -4 -5 -6 -7 0

    o o o i i i i o o i i o o o o o o o o o o o o e

    1 2 3 4 5 6 7 8 -1 -2 -3 -4 -5 -6 -7 0

    c c c i c i i o o c c i i i i i i i i i i i o o o o o c e

    1 2 3 4 5 6 7 8 -1 -2 -3 -4 -5 -6 -7 0

    i i i i i i i i i i i i i i i i i i i i i i i i i i o o o o o e

    1 2 3 4 5 6 7 8 -1 -2 -3 -4 -5 -6 -7 0

    o o o o o o o c c c c c c c c e

    1 2 3 4 5 6 7 8 -1 -2 -3 -4 -5 -6 -7 0

    i i i i i i i i i i i i i i e

    Sample Output

    The pop sequence is: 4 3 2

    The pop sequence is: 4 3 6 5 2 1

    The pop sequence is: 3 2 -6 -5 -4 -3 -2

    The pop sequence is: -7 -6 -5 -4 -3

    The pop sequence is: Empty.

    The pop sequence is: Empty.

    HINT

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    int main()

    {

        Operation myOp;

        int cases;

        cin>>cases;

        for (int i = 0; i < cases; i++)

        {

            myOp.clearData();

            myOp.inputData();

            myOp.inputOpers();

            myOp.showResult();

        }

        return 0;

    }

    作者:7oDo

    仅供参考,请勿抄袭。

    Hang Hang Hang !!!

  • 相关阅读:
    Es module vs require
    phaser3 画虚线实现
    新的计划
    [转]Boostrap Table的refresh和refreshOptions区别
    Storing Java objects in MySQL blobs
    【转】Ubuntu下搜狗输入法突然无法输入中文
    团队作业六
    团队作业七
    团队作业四
    团队作业三
  • 原文地址:https://www.cnblogs.com/Jie-Fei/p/9157509.html
Copyright © 2020-2023  润新知