Data Structure (ZKNU OJ) Weekend Dance (Circular Queue Solution)

Description

Suppose at a weekend dance, the men and women line up as they enter the ballroom. At the beginning of the dance, one person from the head of the men's team and the women's team is assigned as a dance partner. It is stipulated that each dance can have a pair of dancers. If the initial numbers of the two teams are not the same, the unmatched team in the longer team waits for the next dance. Now it is required to write a program to simulate the above-mentioned dance partner matching problem.

Input

The number of men m and the number of women n in the first line (1<=m,n<=100);
the number of dance songs in the second line k (1<=k<=100) .

Output

There are k lines in total, with two numbers in each line, indicating the serial number of the paired dance partner, with men in the front and women in the back.

Sample Input

4 3
6

Sample Output

1 1
2 2
3 3
4 1
1 2
2 3

The title is very simple, the number of dance songs has been told, just use two arrays and traverse them in a loop.

If you use a queue, construct two circular queues to store the numbers of men and women, and then traverse k times to print out the numbers.

Mainly pay attention to the construction of circular queues, practice more, and increase proficiency.

The idea is simple.

Here is the circular queue code implementation, as well as the array code implementation.

#include<stdio.h>
#include<malloc.h>
typedef struct QNode{
	int x;
	struct QNode *next;
}QDataNode;
typedef struct{
	QDataNode *front;
	QDataNode *rear;
}QHeadNode,*LinkQueue;
int InitQueue(LinkQueue *Q);
int EnQueue(LinkQueue *Q,int elem);
int InitQueue(LinkQueue *Q)
{
	LinkQueue qhead=(LinkQueue)malloc(sizeof(QHeadNode));
	QDataNode *vhead=(QDataNode *)malloc(sizeof(QDataNode));
	if(qhead==NULL||vhead==NULL)
	{
		return -1;//Memory allocation failed
	}
	vhead->next=NULL;
	*Q=qhead;
	(*Q)->front=vhead;
	(*Q)->rear=vhead;
	return 0;
}
int EnQueue(LinkQueue *Q,int elem)//Enter the queue
{
	QDataNode *p=(QDataNode *)malloc(sizeof(QDataNode));
	p->x=elem;
	(*Q)->rear->next=p;
	(*Q)->rear=p;
	(*Q)->rear->next=(*Q)->front->next;//Circular queue! ! !
}
intmain()
{
	int m,n,k;
	scanf("%d%d%d",&m,&n,&k);
	//Create two queues to store male and female numbers
	LinkQueue Q1;
	InitQueue(&Q1);
	for(int i=1;i<=m;i++)
	{
		EnQueue(&Q1,i);
	}
	LinkQueue Q2;
	InitQueue(&Q2);
	for(int i=1;i<=n;i++)
	{
		EnQueue(&Q2,i);
	}
	//Use two pointers to traverse two queues to print the number
	QDataNode *p1,*p2;
	p1=Q1->front->next;
	p2=Q2->front->next;
	for(int i=0;i<k;i++)//loop through k times
	{
		printf("%d %d\n",p1->x,p2->x);
		p1=p1->next;
		p2=p2->next;
	}
}

Array code implementation:

#include<stdio.h>
intmain()
{
	int m[100],n[100];
	int M,N,i,k;
	scanf("%d%d%d",&M,&N,&k);
	for(i=1;i<=M;i++)//Array storage number
		m[i]=i;
	for(i=1;i<=N;i++)
	    n[i]=i;
	for(i=1;i<=k;i++)
	{
		if(i<=M)
		printf("%d ",m[i]);
		else
		{
			if(i%M==0)//Remainder control loop
		    printf("%d ",m[M]);
		    else
		    printf("%d ",m[i%M]);
		}
		if(i<=N)
		printf("%d\n",n[i]);
		else
		{
			if(i%N==0)
		    printf("%d\n",n[N]);
		    else
		    printf("%d\n",n[i%N]);
		}
	}
 }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325384741&siteId=291194637