舞伴配对问题java_真心求助【舞伴问题】用JAVA实现

使用Java编程解决舞伴配对问题,通过创建Person结构体表示舞者,包括姓名和性别属性。程序首先初始化男士和女士队列,然后依次将舞者按性别入队。接着,依次匹配并输出舞伴,最后处理可能剩余的未配对舞者。
摘要由CSDN通过智能技术生成

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值