STL-ACboy needs your help again!

Description

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

主要思路:水题,熟悉queue和stack的使用

#include<cstring>
#include<iterator>
#include<stdio.h>
#include<queue>
#include<stack>
using namespace std;
int main()
{
int n, num,val;
scanf("%d",&n);
char form[5],command[5];
while(n--)
{
    queue<int> myqueue;
    stack<int> mystack;
 scanf("%d%s",&num,&form);
 if(strcmp(form,"FIFO")==0){
    while(num--)
    {
    scanf("%s",&command);
    if(strcmp(command,"IN")==0)
    {
        scanf("%d",&val);
        myqueue.push(val);
    }
    else{
        if(myqueue.empty())
        {

            printf("None\n");
        }
        else{
            printf("%d\n",myqueue.front());
            myqueue.pop();
        }
    }


}
 }
 else{
       while(num--)
    {
    scanf("%s",&command);
    if(strcmp(command,"IN")==0)
    {
        scanf("%d",&val);
        mystack.push(val);
    }
    else{
        if(mystack.empty())
        {

            printf("None\n");
        }
        else{
            printf("%d\n",mystack.top());
            mystack.pop();
          }
      }
    }
   }
  }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,967评论 0 23
  • 让心保持平静 认识到作为人的渺小 你是幸运的 智斗妖魔鬼怪 你快乐内心通透,一切都很简单。
    安静的小星星阅读 164评论 5 0
  • 跌跌撞撞走过这三个月的实习,现在回头看似乎一切都清晰了很多,我们抱着这样或那样的目的迷失其中,忘记了初衷,就好像我...
    菜同学少女阅读 297评论 0 1
  • 是不是会想起,曾经 那只小鸟 在你眼前 瞬间飞走 恍惚间 那个小小的村落 飞走多少的小鸟 也包括我这一只
    温州半斤阅读 349评论 0 0
  • 0点刚过,今天是我的第二十八个生日。 过去的日子里,关于生日的美好记忆,屈指可数,没有男朋友的礼物,没有惊喜,没有...
    枫不语阅读 452评论 2 0