public void DancePartner(Person dancer[],int num) { SeqQueue<Person> Mdancers=new SeqQueue<Person>(); SeqQueue<Person> Fdancers=new SeqQueue<Person>(); Mdancers.InitQueue(); //初始化空的男生队列 Fdancers.InitQueue(); //初始化空的女生队列 //把代码补充完整 }
时间: 2023-07-15 09:14:09 浏览: 88
舞伴问题(采用队列实现)
完整代码如下:
public void DancePartner(Person dancer[],int num) {
SeqQueue<Person> Mdancers = new SeqQueue<Person>();
SeqQueue<Person> Fdancers = new SeqQueue<Person>();
Mdancers.InitQueue(); // 初始化空的男生队列
Fdancers.InitQueue(); // 初始化空的女生队列
for(int i = 0; i < num; i++) {
if(dancer[i].getGender() == "male") {
Mdancers.EnQueue(dancer[i]); // 将男生入队
} else {
Fdancers.EnQueue(dancer[i]); // 将女生入队
}
}
while(!Mdancers.IsEmpty() && !Fdancers.IsEmpty()) {
Person male = Mdancers.DeQueue(); // 取出队头的男生
Person female = Fdancers.DeQueue(); // 取出队头的女生
System.out.println(male.getName() + " and " + female.getName() + " are dance partners"); // 输出匹配成功的舞伴
}
if(!Mdancers.IsEmpty()) {
System.out.println(Mdancers.GetLength() + " male dancers are waiting for partners"); // 输出剩余的等待舞伴的男生数
}
if(!Fdancers.IsEmpty()) {
System.out.println(Fdancers.GetLength() + " female dancers are waiting for partners"); // 输出剩余的等待舞伴的女生数
}
}
阅读全文