循环队列解决舞会问题(C++实现)

【题目描述】
假设在周末舞会上,男士们和女士们进入舞厅时,各自排成一队。跳舞开始时,依次从男队和女队的队头上各出一人配成舞伴。规定每个舞曲能有一对跳舞者。若两队初始人数不相同,则较长的那一队中未配对者等待下一轮舞曲。现要求写一个程序,模拟上述舞伴配对问题。

【输入】
第一行两队的人数;

第二行舞曲的数目。

【输出】
配对情况。

【输入样例】
4 6
7
【输出样例】
1 1
2 2
3 3
4 4
1 5
2 6
3 1
 

#include<iostream>
using namespace std;
struct cQueue
{
	int *a=NULL;
	int maxSize;
	int front,rear;
	 
 } ;
void init(cQueue* q, int sz)
{
	q->a=new int[sz];
	q->maxSize=sz;
	q->front=0;
	q->rear=0;
}
void flush(cQueue* q)
{
	delete q->a;	
}
bool empty(cQueue* q)
{
	return q->front==q->rear;
}
bool full(cQueue* q)
{
	return q->front==(q->rear+1)%q->maxSize;
}
void clear(cQueue* q)
{
	q->front=0;
	q->rear=q->front;
}
int size(cQueue* q)
{
	return (q->rear-q->front+q->maxSize)%q->maxSize;
}
bool push(cQueue* q, int x)
{
	if(full(q))return false;
	q->a[q->rear]=x;
	q->rear=(q->rear+1)%q->maxSize;
	return true;
}
void pop(cQueue* q)
{
	q->front=(q->front+1)%q->maxSize;
}
int front(cQueue* q)
{
	return q->a[q->front];	
}
int main()
{
		cQueue m;
	cQueue w;
	cQueue *men=&m;
	cQueue *women=&w;
	int p,q,r;
	cin>>p>>q>>r;
	init(men,1000);
	init(women,1000);
	for(int i=1;i<=p;i++)push(men,i);
	for(int i=1;i<=q;i++)push(women,i);
	for(int i=0;i<r;i++){
		int tM = front(men);	pop(men);
		int tW = front(women);	pop(women);
		cout<<tM<<" "<<tW<<endl;
		push(men, tM);
		push(women, tW);

	}
}

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值