首先,我们需要理解题目的要求。
题目要求模拟舞伴配对问题,舞会开始时,男士和女士各自排成一队,每次从男队和女队的队头各出一个人配成舞伴。如果男队和女队初始人数不相同,则较长的队伍中未配对者等待下一轮舞曲。
我们可以使用队列来模拟这个过程。我们分别创建一个队列来存放男士和女士,每个队列中的元素表示一个人。
接下来,我们根据题目要求编写代码。
首先是使用数组实现的队列:
class CircleQueue {
private int maxSize;
private int front;
private int rear;
private int[] arr;
public CircleQueue(int maxSize) {
this.maxSize = maxSize;
arr = new int[maxSize];
front = 0;
rear = 0;
}
/**
* 判断队列是否满
* @return
*/
public boolean isFull() {
return (rear + 1) % maxSize == front;
}
/**
* 判断队列是否为空
* @return
*/
public boolean isEmpty() {
return rear == front;
}
/**
* 添加数据到队列
* @param n
*/
public void addQueue(int n) {
if (isFull()) {
System.out.println("Queue is full");
return;
}
arr[rear] = n;
rear = (rear + 1) % maxSize;
}
/**
* 获取队列的数据,出队列
* @return
*/
public int getQueue() {
if (isEmpty()) {
throw new RuntimeException("Queue is empty");
}
int value = arr[front];
front = (front + 1) % maxSize;
return value;
}
/**
* 显示队列的所有数据
*/
public void showQueue() {
if (isEmpty()) {
System.out.println("Queue is empty");
return;
}
for (int i = front; i < front + size(); i++) {
System.out.print(arr[i % maxSize] + " ");
}
}
/**
* 求出当前队列有效数据的个数
* @return
*/
public int size() {
return (rear + maxSize - front) % maxSize;
}
/**
* 显示队列的头数据,注意不是取出数据
* @return
*/
public int headQueue() {
if (isEmpty()) {
throw new RuntimeException("Queue is empty");
}
return arr[front];
}
}
public class CircleQueueDemo {
public static void main(String[] args) {
CircleQueue maleQueue = new CircleQueue(1000);
CircleQueue femaleQueue = new CircleQueue(1000);
// 输入男士和女士的人数
int m = 2;
int n = 4;
// 将男士和女士依次加入队列
for (int i = 1; i <= m; i++) {
maleQueue.addQueue(i);
}
for (int i = 1; i <= n; i++) {
femaleQueue.addQueue(i);
}
// 输入舞曲的数目
int k = 6;
// 配对舞伴
for (int i = 0; i < k; i++) {
int male = maleQueue.getQueue();
int female = femaleQueue.getQueue();
System.out.println(male + " " + female);
maleQueue.addQueue(male);
femaleQueue.addQueue(female);
}
}
}
然后是使用链表实现的队列:
class LinkNode {
int data;
LinkNode next;
public LinkNode(int data) {
this.data = data;
}
}
class CircleQueueLink{
LinkNode front;
LinkNode rear;
/**
* 初始化队列
*/
public CircleQueueLink(){
front = rear = null;
}
/**
* 添加数据到队列
* @param data
*/
public void addQueue(int data){
// 创建新的节点
LinkNode newNode = new LinkNode(data);
// 如果队列为空,front和rear都指向新节点;否则,rear指向新节点,新节点指向front(形成循环队列)
if(front == null){
front = rear = newNode;
rear.next = front;
}else{
rear.next = newNode;
rear = newNode;
rear.next = front;
}
}
/**
* 获取队列的数据,出队列
*/
public int getQueue(){
// 如果队列为空,抛出异常;
if(front == null){
throw new RuntimeException("Queue is empty");
}
int value = front.data;
// 如果队列只有一个节点,front和rear都指向null;否则,front指向下一个节点,rear指向front
if(front == rear){
front = rear = null;
}else{
front = front.next;
rear.next = front;
}
return value;
}
/**
* 显示队列的所有数据
*/
public void display(){
LinkNode temp = front;
if(front == null){
System.out.println("Queue is empty");
}else{
while(temp.next != front){
System.out.print(temp.data + " ");
temp = temp.next;
}
System.out.print(temp.data);
System.out.println();
}
}
/**
* 判断队列是否为空
* @return
*/
public boolean isEmpty(){
return front == null;
}
/**
* 求出当前队列有效数据的个数
* @return
*/
public int size(){
int count = 0;
LinkNode temp = front;
if(front == null){
return 0;
}else{
while(temp.next != front){
count++;
temp = temp.next;
}
count++;
}
return count;
}
/**
* 获取队列的数据,不出队列
* @return
*/
public int getFront(){
if(front == null){
throw new RuntimeException("Queue is empty");
}
return front.data;
}
}
public class CircleQueueLinkDemo{
public static void main(String[] args) {
CircleQueueLink maleQueue = new CircleQueueLink();
CircleQueueLink femaleQueue = new CircleQueueLink();
// 输入男士和女士的人数
int m = 2;
int n = 4;
// 将男士和女士依次加入队列
for (int i = 1; i <= m; i++) {
maleQueue.addQueue(i);
}
for (int i = 1; i <= n; i++) {
femaleQueue.addQueue(i);
}
// 输入舞曲的数目
int k = 6;
// 配对舞伴
for (int i = 0; i < k; i++) {
int male = maleQueue.getQueue();
int female = femaleQueue.getQueue();
System.out.println(male + " " + female);
maleQueue.addQueue(male);
femaleQueue.addQueue(female);
}
}
}
以上是使用数组和链表分别实现的队列,你可以根据自己的需求选择其中一种来实现。希望能帮到你,祝你和孩子学习愉快!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。 |