鱼C论坛

 找回密码
 立即注册
查看: 117|回复: 1

求求各位大佬帮帮忙教教孩子咋写,按照springboot3.0一步一步做的,好人一生平安

[复制链接]
发表于 2024-3-21 10:12:52 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
完成以下算法题并提交运行“结果截图”和“代码文件”:

假设在周末舞会上,男士们和女士们进入舞厅时,各自排 成一队。跳舞开始时,依次从男队和女队的队头上各出一人 配成舞伴。规定每个舞曲能有一对跳舞者。若两队初始人数 不相同,则较长的那一队中未配对者等待下一轮舞曲。现要 求写一个程序,模拟上述舞伴配对问题。

第 1 行两个正整数,表示男士人数 m 和女士人数 n,1≤m,n≤1000;

第 2 行一个正整数,表示舞曲的数目 k,k≤1000。

输出共 k 行,每行两个数,之间用一个空格隔开,表示配对舞伴的序号,男士在前,女士在后。

分析:设计两个队列分别存放男士和女士。每对跳舞 的人一旦跳完后就回到队尾等待下次被选。

样例输入:

2 4

6

样例输出:

1 1

2 2

1 3

2 4

1 1

2 2


源码如下:
CircleQueueDemo.java:
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) {
        // 作业代码实现在这里
        
    }
}


CircleQueueLinkDemo.java:
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) {
        // 作业代码实现在这里
    }
}


想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2024-3-21 10:13:46 | 显示全部楼层
首先,我们需要理解题目的要求。

题目要求模拟舞伴配对问题,舞会开始时,男士和女士各自排成一队,每次从男队和女队的队头各出一个人配成舞伴。如果男队和女队初始人数不相同,则较长的队伍中未配对者等待下一轮舞曲。

我们可以使用队列来模拟这个过程。我们分别创建一个队列来存放男士和女士,每个队列中的元素表示一个人。

接下来,我们根据题目要求编写代码。

首先是使用数组实现的队列:

  1. class CircleQueue {
  2.     private int maxSize;
  3.     private int front;
  4.     private int rear;
  5.     private int[] arr;

  6.     public CircleQueue(int maxSize) {
  7.         this.maxSize = maxSize;
  8.         arr = new int[maxSize];
  9.         front = 0;
  10.         rear = 0;
  11.     }

  12.     /**
  13.      * 判断队列是否满
  14.      * @return
  15.      */
  16.     public boolean isFull() {
  17.         return (rear + 1) % maxSize == front;
  18.     }

  19.     /**
  20.      * 判断队列是否为空
  21.      * @return
  22.      */
  23.     public boolean isEmpty() {
  24.         return rear == front;
  25.     }

  26.     /**
  27.      * 添加数据到队列
  28.      * @param n
  29.      */
  30.     public void addQueue(int n) {
  31.         if (isFull()) {
  32.             System.out.println("Queue is full");
  33.             return;
  34.         }
  35.         arr[rear] = n;
  36.         rear = (rear + 1) % maxSize;
  37.     }

  38.     /**
  39.      * 获取队列的数据,出队列
  40.      * @return
  41.      */
  42.     public int getQueue() {
  43.         if (isEmpty()) {
  44.             throw new RuntimeException("Queue is empty");
  45.         }
  46.         int value = arr[front];
  47.         front = (front + 1) % maxSize;
  48.         return value;
  49.     }

  50.     /**
  51.      * 显示队列的所有数据
  52.      */
  53.     public void showQueue() {
  54.         if (isEmpty()) {
  55.             System.out.println("Queue is empty");
  56.             return;
  57.         }
  58.         for (int i = front; i < front + size(); i++) {
  59.             System.out.print(arr[i % maxSize] + " ");
  60.         }
  61.     }

  62.     /**
  63.      * 求出当前队列有效数据的个数
  64.      * @return
  65.      */
  66.     public int size() {
  67.         return (rear + maxSize - front) % maxSize;
  68.     }

  69.     /**
  70.      * 显示队列的头数据,注意不是取出数据
  71.      * @return
  72.      */
  73.     public int headQueue() {
  74.         if (isEmpty()) {
  75.             throw new RuntimeException("Queue is empty");
  76.         }
  77.         return arr[front];
  78.     }
  79. }

  80. public class CircleQueueDemo {
  81.     public static void main(String[] args) {
  82.         CircleQueue maleQueue = new CircleQueue(1000);
  83.         CircleQueue femaleQueue = new CircleQueue(1000);

  84.         // 输入男士和女士的人数
  85.         int m = 2;
  86.         int n = 4;

  87.         // 将男士和女士依次加入队列
  88.         for (int i = 1; i <= m; i++) {
  89.             maleQueue.addQueue(i);
  90.         }
  91.         for (int i = 1; i <= n; i++) {
  92.             femaleQueue.addQueue(i);
  93.         }

  94.         // 输入舞曲的数目
  95.         int k = 6;

  96.         // 配对舞伴
  97.         for (int i = 0; i < k; i++) {
  98.             int male = maleQueue.getQueue();
  99.             int female = femaleQueue.getQueue();
  100.             System.out.println(male + " " + female);
  101.             maleQueue.addQueue(male);
  102.             femaleQueue.addQueue(female);
  103.         }
  104.     }
  105. }
复制代码


然后是使用链表实现的队列:

  1. class LinkNode {
  2.     int data;
  3.     LinkNode next;
  4.     public LinkNode(int data) {
  5.         this.data = data;
  6.     }
  7. }

  8. class CircleQueueLink{
  9.     LinkNode front;
  10.     LinkNode rear;
  11.     /**
  12.      * 初始化队列
  13.      */
  14.     public CircleQueueLink(){
  15.         front = rear = null;
  16.     }
  17.     /**
  18.      * 添加数据到队列
  19.      * @param data
  20.      */
  21.     public void addQueue(int data){
  22.         // 创建新的节点
  23.         LinkNode newNode = new LinkNode(data);
  24.         // 如果队列为空,front和rear都指向新节点;否则,rear指向新节点,新节点指向front(形成循环队列)
  25.         if(front == null){
  26.             front = rear = newNode;
  27.             rear.next = front;
  28.         }else{
  29.             rear.next = newNode;
  30.             rear = newNode;
  31.             rear.next = front;
  32.         }
  33.     }
  34.     /**
  35.      * 获取队列的数据,出队列
  36.      */
  37.     public int getQueue(){
  38.         // 如果队列为空,抛出异常;
  39.         if(front == null){
  40.             throw new RuntimeException("Queue is empty");
  41.         }
  42.         int value = front.data;
  43.         // 如果队列只有一个节点,front和rear都指向null;否则,front指向下一个节点,rear指向front
  44.         if(front == rear){
  45.             front = rear = null;
  46.         }else{
  47.             front = front.next;
  48.             rear.next = front;
  49.         }
  50.         return value;
  51.     }
  52.     /**
  53.      * 显示队列的所有数据
  54.      */
  55.     public void display(){
  56.         LinkNode temp = front;
  57.         if(front == null){
  58.             System.out.println("Queue is empty");
  59.         }else{
  60.             while(temp.next != front){
  61.                 System.out.print(temp.data + " ");
  62.                 temp = temp.next;
  63.             }
  64.             System.out.print(temp.data);
  65.             System.out.println();
  66.         }
  67.     }
  68.     /**
  69.      * 判断队列是否为空
  70.      * @return
  71.      */
  72.     public boolean isEmpty(){
  73.         return front == null;
  74.     }
  75.     /**
  76.      * 求出当前队列有效数据的个数
  77.      * @return
  78.      */
  79.     public int size(){
  80.         int count = 0;
  81.         LinkNode temp = front;
  82.         if(front == null){
  83.             return 0;
  84.         }else{
  85.             while(temp.next != front){
  86.                 count++;
  87.                 temp = temp.next;
  88.             }
  89.             count++;
  90.         }
  91.         return count;
  92.     }
  93.     /**
  94.      * 获取队列的数据,不出队列
  95.      * @return
  96.      */
  97.     public int getFront(){
  98.         if(front == null){
  99.             throw new RuntimeException("Queue is empty");
  100.         }
  101.         return front.data;
  102.     }
  103. }

  104. public class CircleQueueLinkDemo{
  105.     public static void main(String[] args) {
  106.         CircleQueueLink maleQueue = new CircleQueueLink();
  107.         CircleQueueLink femaleQueue = new CircleQueueLink();

  108.         // 输入男士和女士的人数
  109.         int m = 2;
  110.         int n = 4;

  111.         // 将男士和女士依次加入队列
  112.         for (int i = 1; i <= m; i++) {
  113.             maleQueue.addQueue(i);
  114.         }
  115.         for (int i = 1; i <= n; i++) {
  116.             femaleQueue.addQueue(i);
  117.         }

  118.         // 输入舞曲的数目
  119.         int k = 6;

  120.         // 配对舞伴
  121.         for (int i = 0; i < k; i++) {
  122.             int male = maleQueue.getQueue();
  123.             int female = femaleQueue.getQueue();
  124.             System.out.println(male + " " + female);
  125.             maleQueue.addQueue(male);
  126.             femaleQueue.addQueue(female);
  127.         }
  128.     }
  129. }
复制代码


以上是使用数组和链表分别实现的队列,你可以根据自己的需求选择其中一种来实现。希望能帮到你,祝你和孩子学习愉快!

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 14:11

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表