HOJ 1702 ACboy needs your help again!(stack 和 queue,巨水)

stack 和 queue,巨水
本题要点:
1、直接用stl的 stack 和 queue来模拟即可

#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
using namespace std;
int T, n, flag;
char cmd[10];

void solve(bool flag)
{
	queue<int> q;
	stack<int> st;
	int x;
	for(int i = 0; i < n; ++i)
	{
		scanf("%s", cmd);
		if(cmd[0] == 'I')
		{
			scanf("%d", &x);
			if(flag)
				q.push(x);
			else
				st.push(x);
		}else{
			if(flag)
			{
				if(q.empty())
				{
					printf("None\n");
				}else{
					x = q.front();	q.pop();
					printf("%d\n", x);
				}
			}else{
				if(st.empty())
				{
					printf("None\n");
				}else{
					x = st.top(); st.pop();
					printf("%d\n", x);
				}
			}
		}
	}
}

int main()
{
	scanf("%d", &T);
	bool flag;
	while(T--)
	{
		scanf("%d%s", &n, cmd);
		if(strcmp(cmd, "FIFO") == 0)	//队列
		{
			flag = true;	
		}else{	//栈
			flag = false;
		}
		solve(flag);
	}
	return 0;
}

/*
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
*/

猜你喜欢

转载自blog.csdn.net/qq_38232157/article/details/108309368