#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define OK 1
#define OVERFLOW -1
#define ERROR 0
typedef int Status;
int boyscount=0,girlscount=0; //全局变量
int Lcm(int ,int ); //求最小公倍数(lease common multiple)
//-------循环队列-队列的顺序存储结构----
//#define MAXQSIZE 100
typedef int QElemType;
typedef struct {
QElemType *base;
int front;
int rear;
}SqQueue;
//---循环队列的基本算法--------
Status InitQueue(SqQueue &Q,int &MAXQSIZE){
//构造一个空队列
Q.base=(QElemType *)malloc((MAXQSIZE+1) *sizeof(QElemType));
if(!Q.base)exit(OVERFLOW);
Q.front=Q.rear=0;
return OK;
}
int Queuelength(SqQueue Q,int MAXQSIZE){
//返回Q的元素个数,即队列长度
return(Q.rear-Q.front+MAXQSIZE)%MAXQSIZE;
}
Status EnQueue (SqQueue &Q,QElemType e,int MAXQSIZE){
//入队列,插入元素e为Q的新的队尾元素
if((Q.rear+1)%(MAXQSIZE+1)==Q.front) return ERROR; //队列满
Q.base[Q.rear]=e;
Q.rear=(Q.rear+1)%(MAXQSIZE+1);
return OK;
}
Status DeQueue(SqQueue &Q,QElemType &e,int MAXQSIZE){
//若队列不空,则删除Q的对头元素,用e返回其值,并返回OK
//否则返回ERROR
if(Q.front==Q.rear) return ERROR;
e=Q.base[Q.front];
Q.front=(Q.front+1)%(MAXQSIZE);
return OK;
}
void Init(SqQueue &Boys,SqQueue &Girls){
printf("/n『初始化』"); //标题
printf("/n请输入男女生人数,空格分隔:");
scanf("%d%d",&boyscount,&girlscount);
InitQueue(Boys,boyscount);
for(int i=0;i<boyscount;i++){
EnQueue (Boys,i+1,boyscount); //给男生编号123...
}//for
InitQueue(Girls,girlscount);
for(i=0;i<girlscount;i++){
EnQueue (Girls,i+1,girlscount); //给女生编号
}//for
printf("初始化完毕/n");
}//Init
void Match(SqQueue Boys,SqQueue Girls){
printf("/n『输出每曲配对情况』");
if(boyscount==0){
printf("/n 请先初始化!/n");
return;
}//if
int n;
printf("/n请输入曲目数:");
scanf("%d",&n);
int Id_boy,Id_girl;
printf("/n曲目 男 女/n");
for(int i=1;i<=n;i++){
DeQueue(Boys,Id_boy,boyscount);
DeQueue(Girls,Id_girl,girlscount);
printf("%3d %3d <---> %3d/n",i,Id_boy,Id_girl);
//输出每曲配对情况
}//for
}//Match
void AntiMatch(SqQueue Boys,SqQueue Girls){
printf("/n『求满足条件的曲目』");
if(boyscount==0){
printf("/n 请先初始化!/n");
return;
}//if
int x,y;
printf( "/n请输入男女编号,空格分隔:");
scanf("%d%d",&x,&y);
int Id_boy,Id_girl;
for(int i=1;i<=Lcm(boyscount,girlscount);i++){
DeQueue(Boys,Id_boy,boyscount);
DeQueue(Girls,Id_girl,girlscount);
if((Id_boy==x)&&(Id_girl==y)) break;
}//for
if((Id_boy==x)&&(Id_girl==y)){
printf("两人会在第%d曲和第%d曲搭配./n",i,i+Lcm(boyscount,girlscount));
return;
}//if
printf("抱歉,你输入的两人不可能搭配!/n");
}//AntiMatch
int Lcm(int m,int n){//求最小公倍数(lease common multiple)
int temp,p,r;
if(n<m){
temp=n;
n=m;
m=temp; //把大数放在n中,小数放在m中
}//if
p=n*m;
while(m!=0)
{
r=n%m;
n=m;
m=r;
}//while
return p/n;
}//Lcm
//#include "Students.h"
void main(){
SqQueue Boys,Girls;
system("title 学生搭配问题"); //标题
system("color 17");
//设置背景/字体颜色,1为背景,7为前景,其值可随便设,系统默认为07
Begin:
printf("/n");
printf(" ╔════════╗/n");
printf(" ║ 学生搭配问题 ║/n");
printf(" ╚════════╝/n");
printf(" 1.初始化/n");
printf(" 2.输出每曲配对情况/n");
printf(" 3.求满足条件的曲目/n");
printf(" 4.退出/n");
char choice;
switch(choice=getch()){
case '1': Init(Boys,Girls);break; //初始化
case '2': Match(Boys,Girls);break; //输出每曲配对情况
case '3': AntiMatch(Boys,Girls);break; //求满足条件的曲目
case '4': return; //退出
}//switch
goto Begin;
}//main