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!

InputThe 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.OutputFor 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
此题主要是栈和队的应用FIFO表示队先进先出,FILO表示栈先进后出,IN表示向栈或队插入数据,OUT表示从栈顶或队头拿出数据,如果栈或队为空就输出None。。。。
 1 #include <stdio.h>
 2 #include <queue>
 3 #include <stack>
 4 using namespace std;
 5 
 6 void solve(){
 7     int t;
 8     scanf("%d",&t);
 9     stack<int> s;
10     queue<int> q;
11     while(t--){
12         int n,num;
13         char type[5],order[5];
14         scanf("%d%s",&n,&type);
15         if(type[2]=='F'){
16             while(q.size()) q.pop();
17             while(n--){
18                 scanf("%s",&order);
19                 if(order[0]=='I'){
20                     scanf("%d",&num);
21                     q.push(num);
22                 }    
23                 else if(order[0]=='O'){
24                     if(q.size()){
25                         printf("%d\n",q.front());
26                         q.pop();
27                     }
28                     else printf("None\n");
29                 }
30             }
31         }
32         else if(type[2]=='L'){
33             while(s.size()) s.pop();
34             while(n--){
35                 scanf("%s",&order);
36                 if(order[0]=='I'){
37                     scanf("%d",&num);
38                     s.push(num);
39                 }    
40                 else if(order[0]=='O'){
41                     if(s.size()){
42                         printf("%d\n",s.top());
43                         s.pop();
44                     }
45                     else printf("None\n");
46                 }
47             }
48         } 
49     } 
50 }
51 
52 int main(){
53     solve();
54     return 0;
55 } 

 

posted @ 2020-03-26 09:53  梦娃子  阅读(196)  评论(0)    收藏  举报