Title Set data structures and algorithms (Chinese) 7-18 banking simple simulation queue (25 minutes)

1. Topic

There is provided a bank A, B two service windows and service processing speed is not the same, wherein A is 2 times the processing speed of the window B in the window - i.e., when the completion of each window A process customer 2, B processed window 1 a customer. Given the sequence of Customers Bank, please complete the order by business customers of the output sequence. Assuming no customer has to consider the time interval arrives, and when different windows simultaneously processed two customers, A customer priority output window.

Input formats:

Line input is a positive integer, wherein the first number N (≤1000) the total number of customers, followed by N-bit customer number. Odd numbered window A customer needs to conduct business, an even number of customers went to B window. Between numbers separated by a space.

Output formats:

Press transaction completion sequence output from the customer number. Between numbers separated by spaces, but can not have an extra space after the last number.

Sample input:

8 2 1 3 9 4 11 13 15

Sample output:

1 3 2 9 11 4 13 15

2. Topic analysis

Corresponding to A, window B declares two queues, into an odd number of A, B, into the even outputs a B output when the two output A, which finally continue the rest of the remaining two attributes to Hu it

3. Code

#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();
		}
	}


}

 

Published 31 original articles · won praise 1 · views 191

Guess you like

Origin blog.csdn.net/qq_42325947/article/details/104216390