数据结构与算法题目集(中文)7-18 银行业务队列简单模拟 (25分)

1.题目

设某银行有A、B两个业务窗口,且处理业务的速度不一样,其中A窗口处理速度是B窗口的2倍 —— 即当A窗口每处理完2个顾客时,B窗口处理完1个顾客。给定到达银行的顾客序列,请按业务完成的顺序输出顾客序列。假定不考虑顾客先后到达的时间间隔,并且当不同窗口同时处理完2个顾客时,A窗口顾客优先输出。

输入格式:

输入为一行正整数,其中第1个数字N(≤1000)为顾客总数,后面跟着N位顾客的编号。编号为奇数的顾客需要到A窗口办理业务,为偶数的顾客则去B窗口。数字间以空格分隔。

输出格式:

按业务处理完成的顺序输出顾客的编号。数字间以空格分隔,但最后一个编号后不能有多余的空格。

输入样例:

8 2 1 3 9 4 11 13 15

输出样例:

1 3 2 9 11 4 13 15

2.题目分析

对应A、B窗口声明两个队列,奇数放到A中,偶数放到B中,在输出的时候A输出两个B输出一个,最后两个哪个剩下了就继续将剩下的属性胡就可以了

3.代码

#include<iostream>
#include<queue>
using namespace std;
int main()
{
	int amount;
	cin >> amount;
	queue<int>a;
	queue<int>b;
	for (int i = 0; i < amount; i++)
	{
		int temp;
		cin >> temp;
		if (temp % 2 == 0)
			b.push(temp);
		else
			a.push(temp);
	}

	int count = 0;//控制空格的输出
	while (!a.empty() && !b.empty())
	{
		for (int i = 0; i < 2&&!a.empty(); i++)
		{
			if (count == 0)
			{
				cout << a.front();
				a.pop();
				count++;
			}
			else
			{
				cout << " " << a.front();
				a.pop();
			}
		}
	
		if (count == 0)
		{
			cout << b.front();
			b.pop();
			count++;
		}
		else
		{
			cout << " " << b.front();
			b.pop();
		}

	}
	while (!a.empty())
	{
		if (count == 0)
		{
			cout << a.front();
			a.pop();
			count++;
		}
		else
		{
			cout << " " << a.front();
			a.pop();
		}
	}
	while (!b.empty())
	{
		if (count == 0)
		{
			cout << b.front();
			b.pop();
			count++;
			}
		else {
			cout << " " << b.front();
			b.pop();
		}
	}


}
原文地址:https://www.cnblogs.com/Jason66661010/p/12789018.html