STL练习-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
分别定义一个栈和一个队列,输入FIFO即先入先出,操作队列,输入FILO即先入后出,操作栈。
 1 #include<iostream>
 2 #include<stack>
 3 #include<queue>
 4 using namespace std;
 5 int main()
 6 {
 7 stack<int> s;
 8 queue<int> q;
 9 int n;
10 cin>>n;
11   while(n--)
12   {
13    int m;
14    string s1;
15    cin>>m>>s1;
16   while(!s.empty())s.pop();
17   while(!q.empty())q.pop();
18    if(s1=="FIFO")
19    {
20      for(int i=0;i<m;i++)
21      {
22       string s2;int num;
23       cin>>s2;
24       if(s2=="IN"){cin>>num;q.push(num);}
25       if(s2=="OUT"){
26                    if(q.empty())cout<<"None"<<endl;
27                       else
28                       {
29                        cout<<q.front()<<endl;
30                        q.pop();
31                       }
32                    }
33      }
34    }
35    if(s1=="FILO")
36    {
37      for(int i=0;i<m;i++)
38      {
39       string s2;int num;
40       cin>>s2;
41       if(s2=="IN"){cin>>num;s.push(num);}
42       if(s2=="OUT"){
43                    if(s.empty())cout<<"None"<<endl;
44                       else
45                       {
46                        cout<<s.top()<<endl;s.pop();
47                       }
48 
49                    }
50      }
51    }
52 
53   }
54   return 0;
55 }

猜你喜欢

转载自www.cnblogs.com/qq-1423406156/p/11939676.html