hdu 1702 ACboy needs your help again!

ACboy needs your help again!

 

 思路:分两种容器,一个先进先出,一个先进后出,显然一个队列,一个栈,分好情况就行

代码:

#include<iostream>
#include<queue>
#include<stack>

using namespace std;

int main(){

    int m, n, num;
    char str1[15], str2[15];

    cin >> n;
    while (n--){
        scanf("%d %s", &m, str1);
        if (str1[2] == 'F'){
            queue<int>q;
            while (m--){
                scanf("%s", str2);
                if (str2[0] == 'I'){
                    cin >> num;
                    q.push(num);
                }
                else{
                    if (q.empty()){
                        cout << "None"<<endl;
                    }
                    else{
                        cout << q.front() << endl;
                        q.pop();
                    }
                }

            }
        }
        if (str1[2] == 'L'){
            stack<int>s;
            while(m--){
                scanf("%s", str2);
                if (str2[0] == 'I'){
                    cin >> num;
                    s.push(num);
                }
                else{
                    if (s.empty()){
                        cout << "None" << endl;
                    }
                    else{
                        cout << s.top() << endl;
                        s.pop();
                    }
                }
            }
        }
    }


    system("pause");
    return 0;
}
原文地址:https://www.cnblogs.com/pcdl/p/12349896.html