首页 > 其他 > 详细

A - ACboy needs your help again! (HDU - 1702)

时间:2018-02-10 13:35:54      阅读:212      评论:0      收藏:0      [点我收藏+]

- 题目大意

    给出了先进先出和先进后出的两种结构,分别对应队列和栈,并且每种均给出In和Out两类操作,如果是In,push进后面的数,如果是Out,输出栈顶(队首)。

- 解题思路

    对于给的命令判断,然后来决定是用队列还是栈。

- 代码

#include<iostream>
#include<stack>
#include<queue>

using namespace std;
int main()
{
	int n,x,b;
	char a[5];
	char c[5];
	cin >> n;
	while (n--)
	{
		cin >> x >> a;
		
		if (strcmp(a, "FIFO"))
		{
			stack<int>num;
			while (x--)
			{
				cin >> c;
				if (!strcmp(c, "IN"))
				{
					cin >> b;
					num.push(b);
				}
				else
				{
					if (num.empty())
					{
						cout << "None" << endl;
					}
					else
					{
						cout << num.top() << endl;
						num.pop();
					}
				}
			}
		}

			else
			{
				queue<int>sum;
				while (x--)
				{
				cin >> c;
				if (!strcmp(c, "IN"))
				{
					cin >> b;
					sum.push(b);
				}
				else
				{
					if (sum.empty())
					{
						cout << "None" << endl;
					}
					else
					{
						cout << sum.front() << endl;
						sum.pop();
					}
				}
			}
		}
	}
	return 0;
}

  

A - ACboy needs your help again! (HDU - 1702)

原文:https://www.cnblogs.com/alpacadh/p/8438469.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!