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