7-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
解题思路

根据题意A输出两次B输出一次,A输出奇数,B输出偶数,A与B同时处理完毕时,A优先输出。
将奇数存入一个队列,偶数存入一个队列,A处理奇数,B处理偶数,输出两次A,输出一次B的顺序进行输出。按照顺序为AABAABAABAAB….
编写队列类。声明两个队列A、B、C,读入编号,奇数编号压入队列A中,偶数编号压入队列B中。最后对两队列按照上述思路依次进行出队操作

代码
#include <stdio.h>
#include <queue>
#include <iostream>

using namespace std;

#define MAXSIZE 1000

typedef struct
{
	int *date;
	int front;
	int rear;
} SqQueue;

// 初始化队列
int CreateQueue(SqQueue &Q)
{
	Q.date = new int[MAXSIZE];
	if (!Q.date)
		return 0;
	Q.front = Q.rear = 0;
	return 1;
}

// 入队
int Push(SqQueue &Q, int m_date)
{
	if ((Q.rear + 1) % MAXSIZE == Q.front)
		return 0;
	Q.date[Q.rear] = m_date;
	Q.rear = (Q.rear + 1) % MAXSIZE;
}

// 出队
int Pop(SqQueue &Q, int &e)
{
	if (Q.front == Q.rear)
		return 0;
	e = Q.date[Q.front];
	Q.front = (Q.front + 1) % MAXSIZE;
	return 1;
}

int main()
{
	SqQueue A, B;
	CreateQueue(A);// A窗口办理的用户
	CreateQueue(B);//B窗口办理的用户
	int n;
	cin >> n;
	int result[MAXSIZE];// 用于存储输出结果
	int m_date, i = 0;;
	for (i = 0; i < n; i++)
	{
		cin >> m_date;
		if (m_date % 2 != 0)
			Push(A, m_date);
		else
			Push(B, m_date);
	}
	i = 0;
	while ((A.front != A.rear) && (B.front != B.rear))	//既又A又有B  输出两个A 一个B
	{
		Pop(A, result[i]);
		i++;
		Pop(A, result[i]);
		i++;
		Pop(B, result[i]);
		i++;

	}

	while (A.front != A.rear)// 没B了 光输出A
	{
		Pop(A, result[i]);
		i++;
	}
	while (B.front != B.rear)// 没A了 光输出B
	{
		Pop(B, result[i]);
		i++;
	}

	for (i = 0; i < n - 1; i++)
	{
		printf("%d ", result[i]);
	}
	printf("%d", result[n - 1]);
	getchar();
	getchar();
	return 0;
}