使用队列 模拟跳舞舞伴Java实现
题目:
假设在周末舞会上,男士们和女士们进入舞厅时,各自排成一队。
和女队的队头上各出一人配成舞伴。若两队初始人数不相同,则较长的一队未配对者等待下一轮舞曲,现要求写一算法模拟上述舞伴配对问题。
代码解析:
package 课程;
import java.util.Stack;
import java.util.Scanner;
//Tseqqueue为泛型循环队列
import 课程.Tseqqueue;
public class dance {
//跳舞分配 n为跳舞的次数
public static void DancePartner(Person[] dancer, int n) {
Tseqqueue<Person> Mdancers = new Tseqqueue<Person>();
Tseqqueue<Person> Fdancers = new Tseqqueue<Person>();
// 分男队列和女队列
for (int i = 0; i < dancer.length; i++) {
if (dancer[i].getSex().equals("M")) {
Mdancers.EnQueue(dancer[i]);
} else {
Fdancers.EnQueue(dancer[i]);
}
}
//跳舞分配
int M = Mdancers.size();
int F = Fdancers.size();
int num = 0;
Person pM;
Person pF;
while (num < n) {
System.out.println("第" + (num + 1) + "轮跳舞:");
for (int i = 0; i < Math.min(M, F); i++) {
pM = Mdancers.DeQueue();
Mdancers.EnQueue(pM);
pF = Fdancers.DeQueue();
Fdancers.EnQueue(pF);
System.out.println("男士"+pM.getName() + "和" + "女士"+pF.getName() + "一起跳舞");
}
System.out.println("还有"+(Math.max(M, F)-Math.min(M, F))+"人等待跳舞");
num++;
}
}
//分组打印男女队 方法1 使用队列
static public void FMgroup(String[] names, String[] sexs) {
Tseqqueue<String> que = new Tseqqueue<String>();
int num=0;
System.out.println("男士:");
for (int i = 0; i < names.length; i++) {
if (sexs[i].equals("F")) {
que.EnQueue(names[i]);
} else {
System.out.print(names[i]);
}
}
System.out.println();
System.out.println("女士:");
que.print();
}
//分组打印男女队 方法2 使用两个栈
static public void FMgroup2(String[] names, String[] sexs) {
//使用stack存
Stack<String> stack = new Stack<String>();
Stack<String> stack2 = new Stack<String>();
System.out.println("男士:");
int num = 0;
for (int i = 0; i < names.length; i++) {
stack.push(names[i]);
if (sexs[i].equals("M")) {
System.out.print(stack.pop() + " ");
} else {
num++;
}
}
System.out.println();
System.out.println("女士:");
for (int i = 0; i < num; i++) {
stack2.push(stack.pop());
}
for (int i = 0; i < num; i++) {
System.out.print(stack2.pop() + " ");
}
System.out.println();
}
public static void main(String[] args) {
String[] names = { "张", "王", "李", "赵", "孙", "贾", "凉" };
String[] sexs = { "F", "M", "M", "F", "M", "F", "M" };
FMgroup(names, sexs);
// FMgroup2(names, sexs);
Person[] dancers = new Person[names.length];
for (int i = 0; i < names.length; i++) {
dancers[i] = new Person(names[i], sexs[i]);
}
Scanner sc = new Scanner(System.in);
System.out.println("请输入跳舞的次数:");
int n = sc.nextInt();
DancePartner(dancers, n);
}
}
class Person {
private String name;
private String sex;
public Person(String name, String sex) {
this.name = name;
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
提醒:
//Tseqqueue为泛型循环队列 我的Tseqqueue类如下:
package 课程;
public class Tseqqueue<T> {
private static int Maxsize = 5;
public T data[];
public int front,rear;
public Tseqqueue() {
this(Maxsize);
}
public Tseqqueue(int n) {
front=rear=0;
data = (T[])new Object[n];
}
public void EnQueue(T x) {
if (isFull()) {
addapacity();
}
rear = (rear+1) % data.length;
data[rear] = x;
}
public T DeQueue() {
if (isEmpty()) {
System.out.println("empty");
return null;
}
front = (front+1) % data.length;
return data[front];
}
public void addapacity() {
int i,j=1;
if((rear+1)%data.length==front){
T p[] = (T[])new Object[data.length << 1];
if(rear==data.length-1) {
for(i=1;i<=rear;i++) {
p[i]=data[i];
}
}
else {
for(i=front+1;i<data.length;i++,j++) {
p[j]=data[i];
}
for(i=0;i<=rear;i++,j++) {
p[j]=data[i];
}
front=0;
rear = j;
}
data=p;
System.out.println("扩容成功 容量增加至"+data.length);
}
}
public T Gethead() {
return data[front+1%data.length];
}
public boolean isEmpty() {
return rear == front;
}
public void print() {
int j=front;
for(int i=0;i<size();i++) {
j = (j+1)%data.length;
System.out.print(data[j]+" ");
}
System.out.println();
}
public int size() {
// System.out.println("队列长为:" + (rear - front + data.length) % data.length);
return (rear - front + data.length) % data.length;
}
public boolean isFull() {
return front == (rear + 1) % data.length;
}
}