大家好,关于Java数据结构跳舞配对问题(队列的应用)很多朋友都还不太明白,今天小编就来为大家分享关于队列男女跳舞配对的知识,希望对各位有所帮助!
本文目录
课程设计题目1:舞伴配对模拟Java数据结构跳舞配对问题(队列的应用)课程设计题目1:舞伴配对模拟具体算法及相关的类型定义
typedef struct{
char name[20];
char sex;//性别,'F'表示女性,'M'表示男性
}Person;
typedef Person DataType;//将队列中元素的数据类型改为Person
void DancePartner(Person dancer[],int num)
{//结构数组dancer中存放跳舞的男女,num是跳舞的人数。
int i;
Person p;
CirQueue Mdancers,Fdancers;
InitQueue(&Mdancers);//男士队列初始化
InitQueue(&Fdancers);//女士队列初始化
for(i=0;i<num;i++){//依次将跳舞者依其性别入队
p=dancer[i];
if(p.sex=='F')
EnQueue(&Fdancers.p);//排入女队
else
EnQueue(&Mdancers.p);//排入男队
}
printf("The dancing partners are:\n\n");
while(!QueueEmpty(&Fdancers)&&!QueueEmpty(&Mdancers)){
//依次输入男女舞伴名
p=DeQueue(&Fdancers);//女士出队
printf("%s",p.name);//打印出队女士名
p=DeQueue(&Mdancers);//男士出队
printf("%s\n",p.name);//打印出队男士名
}
if(!QueueEmpty(&Fdancers)){//输出女士剩余人数及队头女士的名字
printf("\n There are%d women waitin for the next round.\n",Fdancers.count);
p=QueueFront(&Fdancers);//取队头
printf("%s will be the first to get a partner.\n",p.name);
}else
if(!QueueEmpty(&Mdancers)){//输出男队剩余人数及队头者名字
printf("\n There are%d men waiting for the next round.\n",Mdacers.count);
p=QueueFront(&Mdancers);
printf("%s will be the first to get a partner.\n",p.name);
}
}//DancerPartners
参考程序如下,供大家阅读:
#include<stdio.h>
#include<malloc.h>
#include<string.h>
#define MaxNumber 100
typedef struct{
char name[20];
char sex[4];/*性别,‘F’表示女性,‘M’表示男性*/
}Person;
/*将队列中元素的数据类型改为Person*/
typedef struct
{ Person data[MaxNumber];
int front;
int rear;
}CirQueue;
CirQueue*InitQueue()
{ CirQueue*q;
q=(CirQueue*)malloc(sizeof(CirQueue));
q-> front=q-> rear=0;
return q;
}
int QueueEmpty(CirQueue*q)
{ return(q-> front==q-> rear);
}
int EnQueue(CirQueue*q,Person x)
{ if((q-> rear+1)%MaxNumber==q-> front)
{ printf("\nOverflow!\n");
return 0;
}
q-> data[q-> rear]=x;
q-> rear=(q-> rear+1)%MaxNumber;
return 1;
}
Person DeQueue(CirQueue*q)
{ Person x;
if(q-> front==q-> rear)
{ printf("\nThe queue is empty! Can't delete!\n");
return;
}
x=q-> data[q-> front];
q-> front=(q-> front+1)%MaxNumber;
return x;
}
void DancePartner(Person dancer[],int num)
{/*结构数组dancer中存放跳舞的男女,num是跳舞的人数*/
CirQueue*Mdancers,*Fdancers;
int i, count=0;
Person p;
Mdancers=InitQueue();/*男士队列初始化*/
Fdancers=InitQueue();/*女士队列初始化*/
for(i=0;i<num;i++)/*依次将跳舞者依其性别入队*/
{
p=dancer[i];
if(strcmp(p.sex,"f")==0)
EnQueue(Fdancers,p);/*排入女队*/
else EnQueue(Mdancers,p);/*排入男队*/
}
printf("\nThe dancing partners are:\n");
while(!QueueEmpty(Fdancers)&&!QueueEmpty(Mdancers))
{/*依次输入男女舞伴名*/
count++;
p=DeQueue(Fdancers);/*女士出队*/
printf("%s\t",p.name);/*打印出队女士名*/
p=DeQueue(Mdancers);/*男士出队*/
printf("%s\n",p.name);/*打印出队男士名*/
}
if(!QueueEmpty(Fdancers))/*输出女士剩余人数及队头女士的名字*/
{
printf("\n There are%d women waiting for the next round.\n
",num-2*count);
p=DeQueue(Fdancers);
printf("%s will be the first to get a partner.\n",p.name);
printf("\n");
}
if(!QueueEmpty(Mdancers))/*输出男队剩余人数及队头者名字*/
{
printf("\n There are%d men waiting for the next round.\n
",num-2*count);
p=DeQueue(Mdancers);
printf("%s will be the first to get a partner.\n",p.name);
printf("\n");
}
}
int GetDancersInfo(Person dancer[])
{ int count=0;
Person p;
while(1)
{ printf("Input the sex:\n");
scanf("%s",p.sex);
if(strcmp(p.sex,"0")==0) break;
printf("Input the name:\n");
scanf("%s",p.name);
dancer[count]=p;
count++;
}
return count;
}
void main()
{ int DancersNum;
Person Dancers[MaxNumber];
DancersNum=GetDancersInfo(Dancers);
if(DancersNum!=0) DancePartner(Dancers,DancersNum);
getch();
}
Java数据结构跳舞配对问题(队列的应用)代码如下,可以直接运行。
public static void main(String[] args){
final int M= 6;// number of girls,可改动
final int N= 7;// number of boys,可改动
int x= 3;// some boy,可改动
int y= 5;// some girl,可改动
String result="";//记录结果,即第二个问题
//初始化,假设队列存放男女生编号,从1开始
Queue<Integer> boys= new LinkedList<Integer>();
for(int i= 1; i<= N; i++){
boys.add(i);
}
Queue<Integer> girls= new LinkedList<Integer>();
for(int i= 1; i<= M; i++){
girls.add(i);
}
//跳舞开始
int min= boys.size()> girls.size()? girls.size(): boys.size();
int k= 1;// songs
int count= 2;//求出两个值,可改动
while(k< 1000){//为了不死循环,这里假设最多有999支舞蹈
System.out.println("***This is the"+ k+"st dance:");
for(int i= 0; i< min; i++){
//跳舞,第一个问题:输出每曲配对情况
System.out.println("Boy"+ boys.peek()+"<=> Girl"
+ girls.peek());
//跳过的排到对尾
int boy= boys.remove();
boys.add(boy);
int girl= girls.remove();
girls.add(girl);
//判断 x和y跳舞了没有
if(boy== x&& girl== y){
result+= k+",";
count--;
}
}
if(count== 0)
break;
// next dance
k++;
}
//结果
if(count== 0)
System.out.println("\n***Boy"+ x+" and Girl"+ y
+" dance together in:"+ result);//第二个问题的解答,跳了哪几支舞
else
System.out.println("\n***Boy"+ x+" and Girl"+ y
+" have no chance to dance!");//第二个问题的解答,两人没机会跳舞
}
END,本文到此结束,如果可以帮助到大家,还望关注本站哦!