HDOJ-1702 栈和队列问题[或模拟]

ACboy needs your help again!

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1697    Accepted Submission(s): 906


Problem 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
 

 

Source
 

 

Recommend
lcy
 
 
 
code1:栈和队列的使用
 1 #include<iostream>
 2 #include<stack>
 3 #include<queue>
 4 using namespace std;
 5 int main()
 6 {
 7     char str[12];
 8     int n,m,cases;
 9     scanf("%d",&n);
10     while(n--)
11     {
12         scanf("%d%str",&cases,str);
13         if(str[2]=='F')                   //队列
14         {
15             queue<int>Que;                       //创建一个int型队列
16             while(cases--)
17             {
18                 scanf("%s",str);
19                 if(str[0]=='I')
20                 {
21                     scanf("%d",&m);
22                     Que.push(m);                   //入队
23                 }
24                 else if(!Que.empty())
25                 {
26                     printf("%d\n",Que.front());
27                     Que.pop();                     //出队
28                 }
29                 else
30                     printf("None\n");
31             }
32         }
33         else
34         {
35             stack<int>Sta;                         //创建一个int型的栈
36             while(cases--)
37             {
38                 scanf("%s",str);
39                 if(str[0]=='I')
40                 {
41                     scanf("%d",&m);
42                     Sta.push(m);
43                 }
44                 else if(!Sta.empty())
45                 {
46                     printf("%d\n",Sta.top());
47                     Sta.pop();
48                 }
49                 else
50                     printf("None\n");
51             }
52         }
53     }
54     return 0;
55 }

 
code2:模拟
 1 #include<iostream>
 2 using namespace std;
 3 
 4 int data[10002];
 5 char str[5];
 6 char ord[5];
 7 
 8 int main()
 9 {
10     int n;
11     int i;
12     int cases;
13     int cnt;
14     int num;
15     int front;
16     while(~scanf("%d",&n))
17     {
18         while(n--)
19         {
20             memset(data,0,sizeof(data));
21             scanf("%d%s",&cases,str);
22             if(str[2]=='L')
23             {
24                 cnt=0;
25                 for(i=0;i<cases;i++)
26                 {
27                     scanf("%s",ord);
28                     if(ord[0]=='I')
29                     {
30                         scanf("%d",&num);
31                         data[cnt]=num;
32                         cnt++;
33                     }
34                     if(ord[0]=='O')
35                     {    
36                         if(cnt>0)
37                         {
38                             printf("%d\n",data[cnt-1]);
39                             cnt--;
40                         }
41                         else
42                             printf("None\n");
43                     }
44                 }
45             }
46             else
47             {
48                 cnt=0;
49                 front=0;
50                 for(i=0;i<cases;i++)
51                 {
52                     scanf("%s",ord);
53                     if(ord[0]=='I')
54                     {
55                         scanf("%d",&num);
56                         data[cnt]=num;
57                         cnt++;
58                     }
59                     if(ord[0]=='O')
60                     {
61                         if(front<cnt)
62                         {
63                             printf("%d\n",data[front]);
64                             front++;
65                         }
66                         else
67                             printf("None\n");
68                     }
69                 }
70             }
71         }
72     }
73     return 0;
74 }
75 /*
76 4
77 7 FIFO
78 IN 1
79 IN 3
80 IN 5
81 OUT
82 OUT
83 OUT
84 OUT
85 */

 

 
VhdManager.exe VhdManager.ini ; Generated (13.09.2015 18:44:54) by Simple Vhd Manager v1.0 ; www.sordum.org [Main] Language=Auto WinSetOnTop=0 GridLines=1 NewVhdSize=2,5 NewVhdOpt=0 [VirtualDiskFiles] 1=C:\20180619_034757.vhd [Language_English] 01="BlueLife" 02="Attach marked and detach unmarked files" 03="Add file to list" 04="Remove file from list" 05="Refresh list" 06="Disk Management" 07="Action\n Location\n Attached\n Boot Menu\n Virtual Size" 08="&File;\n &Edit;\n &Options;\n Tools \n &Help;" 09="&Add; File" 10="&Create; and Attach VHD" 11="&Attach; Marked Files" 12="&Detach; All Files" 13="&Remove; Entries In Boot Menu" 14="&Exit;" 15="&Attach;" 16="&Detach;" 17="&Read;-Only" 18="&Add; to Boot Menu" 19="&Remove; From Boot Menu" 20="&Paste; File" 21="&Remove;" 22="&Remove; All" 23="&Check; - Uncheck" 24="&Check; All" 25="&Uncheck; All" 26="&Open; Location" 27="&File; Properties" 28="&Disk;" 29="&Cd;-Rom" 30="&Refresh;" 31="&Open; With Explorer" 32="&Change; Drive Letter" 33="&Remove; Letter" 34="&Drive; Properties" 35="&Attach; Marked To System Startup" 36="&Disable; Autorun" 37="&Always; On Top" 38="&Show; Grid Lines" 39="&Add; to Context Menu" 40="&Show; Hidden Files" 41="&Show; File Extensions" 42="Disk &Management;" 43="Diskpart &Command; Prompt" 44="&Msconfig;" 45="&Restart; Windows Explorer" 46="&Donate;" 47="&HomePage;" 48="&Contact;" 49="&About;" 50="Please Wait ..." 51="Select Virtual Disk File\n Virtual Disk Files\n All Files" 52="Enter boot menu name\nOk\nCancel" 53="Added" 54="Unable to write file!" 55="File not found!" 56="The file path must not contain any spaces. Please correct it and try again." 57="Virtual hard disk size \nVirtual hard disk format \n Virtual hard disk type \nDynamically expanding \n Fixed" 58="%drive% not enough free space!\n Do you wish to continue?" 59="An error occurred. Check your settings and try again! \nError Code:%error%" 60="Mount &Options;\n &Mount; \n &Unmount; \n &Open; With Simple VHD Manager\n VHD Mount \n V
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值