ACboy needs your help again!HDU - 1702
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先进后出,栈。
代码如下:
#include<cstdio>
#include<queue>
#include<stack>
#include<cstring>
using namespace std;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
stack<int> s; //栈
queue<int> q; //队列
int n,k;
char str[6],s1[5];
scanf("%d %s",&n,str);
if(strcmp(str,"FIFO")==0) //队列
{
while(n--)
{
// getchar();
scanf("%s",s1);
if(s1[0]=='I') //进队列
{
scanf("%d",&k);
q.push(k);
}
else if(s1[0]=='O')
{
if(q.empty() ) //队列为空
printf("None\n");
else
{
int a=q.front() ; //返回队列首元素的值
printf("%d\n",a);
q.pop() ; //弹出队列首元素 但不返回其值
}
}
}
}
else if(strcmp(str,"FILO")==0) //栈
{
while(n--)
{
// getchar();
scanf("%s",s1);
if(s1[0]=='I')
{
scanf("%d",&k);
s.push(k); //进栈
}
else if(s1[0]=='O')
{
if(s.empty() ) //栈为空
printf("None\n");
else
{
int b=s.top() ; //返回栈顶元素的值
printf("%d\n",b);
s.pop() ; //弹出栈顶元素,但不返回其值
}
}
}
}
}
return 0;
}
The first line has one integer,represent the number oftest cases.
And the input of each subproblem are described above.
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
1 2 2 1 1 2 None 2 3
思路分析:
FIFO先进先出,队列。FILO先进后出,栈。
代码如下:
#include<cstdio> #include<queue> #include<stack> #include<cstring> using namespace std; int main() { int t; scanf("%d",&t); while(t--) { stack<int> s; //栈 queue<int> q; //队列 int n,k; char str[6],s1[5]; scanf("%d %s",&n,str); if(strcmp(str,"FIFO")==0) //队列 { while(n--) { // getchar(); scanf("%s",s1); if(s1[0]=='I') //进队列 { scanf("%d",&k); q.push(k); } else if(s1[0]=='O') { if(q.empty() ) //队列为空 printf("None\n"); else { int a=q.front() ; //返回队列首元素的值 printf("%d\n",a); q.pop() ; //弹出队列首元素 但不返回其值 } } } } else if(strcmp(str,"FILO")==0) //栈 { while(n--) { // getchar(); scanf("%s",s1); if(s1[0]=='I') { scanf("%d",&k); s.push(k); //进栈 } else if(s1[0]=='O') { if(s.empty() ) //栈为空 printf("None\n"); else { int b=s.top() ; //返回栈顶元素的值 printf("%d\n",b); s.pop() ; //弹出栈顶元素,但不返回其值 } } } } } return 0; }