【HDU】1702---ACboy needs your help again!(栈&&队列)

面对怪物设置的谜题,玩家需运用栈和队列原理解决一系列命令问题,从“FIFO”和“FILO”中找到正确的出入顺序,才能成功营救被囚禁的ACboy。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值