HDU 1702 - ACboy needs your help again!

ACboy was kidnapped!!
he miss his mother very much and is very scare now.You can't image how dark the room he was put into is, so poor :(.
As a smart ACMer, you want to get ACboy out of the monster's labyrinth.But when you arrive at the gate of the maze, the monste say :" I have heard that you are very clever, but if can't solve my problems, you will die with ACboy."
The problems of the monster is shown on the wall:
Each problem's first line is a integer N(the number of commands), and a word "FIFO" or "FILO".(you are very happy because you know "FIFO" stands for "First In First Out", and "FILO" means "First In Last Out").
and the following N lines, each line is "IN M" or "OUT", (M represent a integer).
and the answer of a problem is a passowrd of a door, so if you want to rescue ACboy, answer the problem carefully!

Input

The input contains multiple test cases.
The first line has one integer,represent the number oftest cases.
And the input of each subproblem are described above.

Output

For each command "OUT", you should output a integer depend on the word is "FIFO" or "FILO", or a word "None" if you don't have any integer.

Sample Input

4
4 FIFO
IN 1
IN 2
OUT
OUT
4 FILO
IN 1
IN 2
OUT
OUT
5 FIFO
IN 1
IN 2
OUT
OUT
OUT
5 FILO
IN 1
IN 2
OUT
IN 3
OUT

Sample Output

1
2
2
1
1
2
None
2
3

題意:棧和隊(duì)列的模擬,棧(先進(jìn)后出),隊(duì)列(先進(jìn)先出)

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

using namespace std;

int t, n;
void Stack() {
    char c[5];
    int x;
    stack<int> s;
    for (int  i = 0; i < n; i++)
    {
        scanf("%s", c);
        if (!strcmp(c,"IN"))
        {
            scanf("%d", &x);
            s.push(x);

        }
        else if (!s.empty())
        {
            printf("%d\n", s.top());
            s.pop();
        }
        else printf("None\n");
    }

}

void Queue() {
    char c[5];
    int x;
    queue<int>q;
    for (int i = 0; i < n; i++)
    {
        scanf("%s", c);
        if (!strcmp(c,"IN"))
        {
            scanf("%d", &x);
            q.push(x);
        }
        else if (!q.empty())
        {
            printf("%d\n", q.front());
            q.pop();
        }
        else printf("None\n");
    }
}
int main() {
    char c[10];
    scanf("%d", &t);
    while (t--)
    {
        scanf("%d%s", &n, c);
        if (!strcmp(c, "FIFO")) Queue();
        else Stack();

    }
    return 0;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,090評論 0 23
  • 很久以前 大概19歲 癡迷過安妮寶貝的文字 如今都29了 那些故事也忘得差不多了 七月與安生是我比較喜歡的故事之一...
    仟維閱讀 305評論 0 1
  • 我所有的自負(fù)皆來自我的自卑,所有的英雄氣概都來自于我的軟弱。嘴里振振有詞是因?yàn)樾睦餄M是懷疑,深情是因?yàn)橥春拮约簾o情...
    Gapeng閱讀 11,117評論 0 5
  • 1. 這是一篇不正式的書評。 寫給我最為敬愛的活著的中國作家,阿丁。 他的新書《職業(yè)撒謊者的供述》發(fā)布會,我去了,...
    某人李下閱讀 1,934評論 13 28
  • (一) 正值花開,爭相斗妍。 “真香??!” 他嗅了嗅。 “是啊!” 她指了指一片花海。 “我說的是你?!?倆人相視...
    發(fā)芽的種子娜閱讀 514評論 8 13

友情鏈接更多精彩內(nèi)容