0
点赞
收藏
分享

微信扫一扫

大学数据结构实验(三.栈和队列的应用二)


大学程序实验.数据结构.栈和队列的应用一.舞伴配对问题

  • ​​0 目录​​
  • ​​3 栈和队列的应用​​
  • ​​3.2 舞伴配对问题​​
  • ​​3.2.1 题目​​
  • ​​3.2.2 源码​​
  • ​​1.1.3 下载​​

  • ​​2 下一章​​

0 目录

3 栈和队列的应用

3.2 舞伴配对问题

3.2.1 题目

n个男生和m个女生排成两队列进行配对跳舞,男女队列依次各出一人配成一对舞伴,要求每一首舞曲最多出k对舞伴,没法配对的人只能等待下一首舞曲。跳完后男女依次排到队列最后。打印前t首舞曲的配对情况。n, m, k, t从键盘输入。
要求用队列(顺序队列或链式队列)来实现。

3.2.2 源码

// 舞伴配对问题.cpp : Defines the entry point for the console application.
//
/***********************************************************************
* 头文件包含
***********************************************************************/
#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"

/***********************************************************************
* 本地宏定义
***********************************************************************/
#define OK 1
#define ERROR 0

typedef int QElemType;
typedef int Status;
typedef struct SqQueue
{
QElemType *base;
int front;
int rear;
int flag;
}SqQueue;

/*********************************************************************
* 函 数 名 : main
* 函数功能 : 主函数
* 参数列表 :
* 函数输出 :
*********************************************************************/
Status main()
{
Status QueueMenu();
Status InitQueue(SqQueue &Q,QElemType Value);
Status EnQueue(SqQueue &Q , QElemType e , QElemType Value);
Status DeQueue(SqQueue &Q , QElemType Value);
Status EmptyQueue(SqQueue &Q);
Status QueueCheak(SqQueue &QM,SqQueue &QW,int Value,int MC,int NC);

int m,n,k,t;
QueueMenu();

printf("请输入男生人数:");
scanf("%d",&m);

printf("请输入女生人数:");
scanf("%d",&n);

printf("请输入每曲对数:");
scanf("%d",&k);

printf("请输入舞曲数目:");
scanf("%d",&t);

SqQueue M;
InitQueue(M,m);
SqQueue W;
InitQueue(W,n);

int i,s;
for(i = 1;i <= m;i++)
{
EnQueue(M,i,m);
}
for(i = 1;i <= n;i++)
{
EnQueue(W,i,n);
}

int value=0;
int x,y;
int max,min;
max=m>=n?m:n;
min=m<=n?m:n;

printf("男生 女生\n");
for(i = 1;i <= t;i++)
{
printf("第%d首:\n",i);
for(int j = 1;j <= k;j++)
{
if(!EmptyQueue(M) && !EmptyQueue(W))
{
x=DeQueue(M,m);
y=DeQueue(W,n);
printf(" %d %d\n",x,y);
value++;
}
else
{
break;
}
}

if(k<max)
{
x++;
y++;
printf("还未配对的有:\n");
for(s=k+1;s<=max;s++)
{
if(s>m)
{
x=0;
}
if(s>n)
{
y=0;
}
if(x<=m&&y<=n)
{
printf(" %d %d\n",x,y);
}
else
{
printf(" %d %d\n",x%m,y%n);
}
x++;
y++;
}
}

QueueCheak(M,W,value,m,n);
value = 0;
printf("\n");
}

return OK;
}

/*********************************************************************
* 函 数 名 : QueueMenu
* 函数功能 : 目录函数
* 参数列表 :
* 函数输出 :
*********************************************************************/
Status QueueMenu()
{
printf("======舞伴匹配问题======\n");
return OK;
}

/*********************************************************************
* 函 数 名 : InitQueue
* 函数功能 : 初始化函数
* 参数列表 :
* 函数输出 :
*********************************************************************/
Status InitQueue(SqQueue &Q,QElemType Value)
{
Q.base = (QElemType *)malloc(Value * sizeof(QElemType));
if(!Q.base)
{
return ERROR;
}

Q.front = Q.rear = 0;
Q.flag = 0;
return OK;
}

/*********************************************************************
* 函 数 名 : EnQueue
* 函数功能 : 入队函数
* 参数列表 :
* 函数输出 :
*********************************************************************/
Status EnQueue(SqQueue &Q , QElemType e , QElemType Value)
{
if(Q.front == Q.rear && Q.flag == 1)
{
return ERROR;
}

Q.base[Q.rear] = e;
Q.rear = (Q.rear + 1)%Value;

if(Q.front == Q.rear)
{
Q.flag = 1;
}
return OK;
}

/*********************************************************************
* 函 数 名 : DeQueue
* 函数功能 : 出队函数
* 参数列表 :
* 函数输出 :
*********************************************************************/
Status DeQueue(SqQueue &Q , QElemType Value)
{
QElemType e;
if(Q.front == Q.rear && Q.flag == 0)
{
return ERROR;
}

e = Q.base[Q.front];
Q.front = (Q.front + 1)%Value;

if(Q.front == Q.rear)
{
Q.flag = 0;
}
return e;
}

/*********************************************************************
* 函 数 名 : EmptyQueue
* 函数功能 : 检测空队函数
* 参数列表 :
* 函数输出 :
*********************************************************************/
Status EmptyQueue(SqQueue &Q)
{
if(Q.front == Q.rear && Q.flag == 0)
{
return OK;
}
else
{
return ERROR;
}
}

/*********************************************************************
* 函 数 名 : QueueCheak
* 函数功能 : 检测函数
* 参数列表 :
* 函数输出 :
*********************************************************************/
Status QueueCheak(SqQueue &QM,SqQueue &QW,int Value,int MC,int NC)
{
Status EnQueue(SqQueue &Q , QElemType e , QElemType Value);
Status EmptyQueue(SqQueue &Q);

int i;
if(EmptyQueue(QM))
{
for(i = 1;i <= MC;i++)
{
EnQueue(QM,i,MC);
}
}
if(!EmptyQueue(QM))
{
for(i = 1;i <= Value;i++)
{
QM.rear = (QM.rear + 1)%MC;
}
}
if(EmptyQueue(QW))
{
for(i = 1;i <= NC;i++)
{
EnQueue(QW,i,NC);
}
}
if(!EmptyQueue(QW))
{
for(i = 1;i <= Value;i++)
{
QW.rear = (QW.rear + 1)%NC;
}
}
return OK;
}

1.1.3 下载

链接地址: ​​3.2_舞伴配对问题.cpp​​



举报

相关推荐

0 条评论