hdu1702 隊列與棧

題意:

"FIFO" 則先進先出

 "FILO"則先進後出

赤裸裸~

直接用了stl裏的queue和stack

#include<stdio.h>
#include<queue>
#include<stack>
#include<string.h>

using namespace std;

void que(int n)
{
    queue<int> Q;
    char order[10];
    int a;
    while(n--)
    {
        scanf("%s",order);
        if(!strcmp(order,"IN"))
        {
            scanf("%d",&a);
            Q.push(a);
        }
        else if(!strcmp(order,"OUT"))
        {
            if(Q.empty())
                printf("None\n");
            else
            {
                printf("%d\n",Q.front());
                Q.pop();
            }
        }
    }
}

void sta(int n)
{
    stack<int> s;
    char order[4];
    int a;
    while(n--)
    {
        scanf("%s",order);
        if(order[0]=='I')
        {
            scanf("%d",&a);
            s.push(a);
        }
        else
        {
            if(s.empty())
                printf("None\n");
            else
            {
                printf("%d\n",s.top());
                s.pop();
            }
        }
    }
}

int main()
{
    int T,n,order;
    char str[5];
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%s",&n,str);
        if(str[2]=='F')//fifo
            que(n);
        else
            sta(n);
    }
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章