题目描述
假设在周末舞会上,男士们和女士们进入舞厅时,各自排成一队。跳舞开始时,依次从男队和女队的队头上各出一人配成舞伴。规定每个舞曲能有一对跳舞者。若两队初始人数不相同,则较长的那一队中未配对者等待下一轮舞曲。现要求写一个程序,模拟上述舞伴配对问题。
输入
输入共一行,包含三个数,N,M,K,分别代表男生,女生,舞曲数目,大小均小于100.
输出
输出共K行,每行两个数,表示每一首舞曲参加跳舞人的编号
样例输入
1 1 1
样例输出
1 1
#include<stdio.h>
#include<algorithm>
using namespace std;
int n,m,i,j,k;
struct node{
int x;
node *next;
node(){
next=NULL;
}
}*p,*q,*f1,*f2,*t1,*t2;
int main(){
scanf("%d%d%d",&n,&m,&k);
f1=new node; t1=new node;
f1->x=1;
p=new node;
p=f1;
for (i=2;i<=n;i++){
q=new node;
q->x=i;
p->next=q;
p=q;
}
t1=q;
f2=new node; t2=new node;
f2->x=1;
p=new node;
p=f2;
for (i=2;i<=m;i++){
q=new node;
q->x=i;
p->next=q;
p=q;
}
t2=q;
for (i=1;i<=k;i++){
printf("%d %d\n",f1->x,f2->x);
p=f1;
f1=f1->next;
t1->next=p;
t1=p;
p=f2;
f2=f2->next;
t2->next=p;
t2=p;
}
return 0;
}