(四)约瑟夫问题-单向环形链表

约瑟夫问题

  •   问题描述:
      		 设编号为 1,2,… n 的 n 个人围坐一圈,约定编号为 k(1<=k<=n)的人从 1 开始报数,数到m 的那个人出列,
      		 它的下一位又从 1 开始报数,数到 m 的那个人又出列,依次类推,直到所有人出列为止,由此产生一个出队编号的序列。
    

在这里插入图片描述在这里插入图片描述

在这里插入图片描述
思路:用一个不带头结点的循环链表来处理 Josephu 问题:先构成一个有 n 个结点的单循环链表,然后由 k 结点起从 1 开
始计数,计到 m 时,对应结点从链表中删除,然后再从被删除结点的下一个结点又从 1 开始计数,直到最后一个结点从链表中删除算法结束。

package com.atheima.linkedlist;

public class Josepfu {
    public static void main(String[] args) {
        //测试单向环形链表
        CircleSingleLinkedList circleSingleLinkedList=new CircleSingleLinkedList();
        circleSingleLinkedList.addBoy(5);
        circleSingleLinkedList.showBoy();

        //测试出圈
        circleSingleLinkedList.countBoy(1,2,5);//2->4->1->5->3
    }
}

//创建一个单向的环形链表,
class CircleSingleLinkedList{
    //创建一个first节点
    private Boy first=null;
    //添加节点,构建成一个环形的链表
    public void addBoy(int nums){
        //nums做一个数据校验
        if (nums<1){
            System.out.println("nums的值不正确");
            return;
        }
        Boy curBoy=null;//辅助指针,帮助构建环形链表
        //for循环创建环形链表
        for (int i = 1; i <=nums; i++) {
            //根据编号,创建节点
            Boy boy=new Boy(i);
            //如果是第一个
            if (i==1){
                first=boy;
                first.setNext(first);
                curBoy=first;
            }else {
                curBoy.setNext(boy);
                boy.setNext(first);
                curBoy=boy;
            }
        }

    }

    //遍历当前环形链表
    public void showBoy(){
        if (first==null){
            System.out.println("链表为空");
            return;
        }
        //first不能动,因此使用辅助指针完成遍历
        Boy curBoy=first;
        while (true){
            System.out.printf("编号%d\n",curBoy.getNo());
            if (curBoy.getNext()==first){
                break;
            }
            curBoy=curBoy.getNext();//指针后移
        }
    }

    //根据用户的输入,计算出圈顺序

    /**
     *
     * @param startNo 从第几个小孩开始数数
     * @param countNum 表示数几下
     * @param nums 表示最初有几个小孩在圈中
     */
    public void countBoy(int startNo,int countNum,int nums){
        //对数据校验
        if (first==null || startNo<1 ||startNo>nums){
            System.out.println("参数输入有误,重新输入");
            return;
        }
        //创建辅助指针,帮助完成小孩出圈
        Boy helper=first;
        //辅助指针事先指向环形链表的最后一个节点
        while (true){
            if (helper.getNext()==first){
                //说明helper指向最后一个节点
                break;
            }
            helper=helper.getNext();
        }

        //在报数之前,先让first和helper移动k-1次
        for (int j=0;j<startNo-1;j++){
            first=first.getNext();
            helper=helper.getNext();
        }
        //当小孩报数时,让first和helper指针同时移动m-1次,然后出圈
        //这是一个循环操作,直到圈中只有一个节点
        while (true){
            if (helper==first){
                //说明圈中只有一个节点
                break;
            }
            //让first和helper指针同时移动countNum-1次,然后出圈
            for (int j=0;j<countNum-1;j++){
                first=first.getNext();
                helper=helper.getNext();
            }
            System.out.printf("小孩%d出圈\n",first.getNo());
            //first指向的节点出圈
            first=first.getNext();
            helper.setNext(first);
        }
        System.out.printf("最后留在圈中的小孩编号%d\n",first.getNo());


    }
}

//创建一个Boy类,表示一个节点
class Boy{
    private int no;//编号
    private Boy next;//指向下一个节点,默认null
    public Boy(int no){
        this.no=no;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public Boy getNext() {
        return next;
    }

    public void setNext(Boy next) {
        this.next = next;
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,我可以帮你解答这个问题。 首先,让我们来了解一下约瑟夫问题的背景。约瑟夫问题是一个经典的问题,据传是由古代历史学家约瑟夫斯(Josephus)提出的。问题的具体描述如下: 有n个人围成一圈,从第一个人开始报数,报到m的人出圈,剩下的人继续从1开始报数,直到剩下最后一个人。求出最后一个出圈的人的编号。 为了解决这个问题,我们可以使用单向循环链表来实现。下面是一个简单的C++单向循环链表的实现,包含了三个文件:Node.h、List.h和List.cpp。 首先是Node.h文件,用来定义节点类: ```cpp // Node.h #ifndef NODE_H #define NODE_H class Node { public: int data; Node* next; Node(int data) { this->data = data; next = nullptr; } }; #endif ``` 然后是List.h文件,用来定义链表类: ```cpp // List.h #ifndef LIST_H #define LIST_H #include "Node.h" class List { public: List(); ~List(); void insert(int data); void remove(Node* node); Node* getHead(); Node* getNext(Node* node); private: Node* head; }; #endif ``` 最后是List.cpp文件,用来实现链表类的方法: ```cpp // List.cpp #include "List.h" List::List() { head = nullptr; } List::~List() { Node* curr = head; while (curr != nullptr) { Node* temp = curr; curr = curr->next; delete temp; } } void List::insert(int data) { Node* node = new Node(data); if (head == nullptr) { head = node; head->next = head; } else { Node* curr = head; while (curr->next != head) { curr = curr->next; } curr->next = node; node->next = head; } } void List::remove(Node* node) { Node* curr = head; if (node == head) { head = head->next; } else { while (curr->next != node) { curr = curr->next; } curr->next = node->next; } delete node; } Node* List::getHead() { return head; } Node* List::getNext(Node* node) { return node->next; } ``` 现在我们就可以使用这个单向循环链表来解决约瑟夫问题了。下面是一个简单的解决方案: ```cpp #include <iostream> #include "List.h" int josephus(int n, int m) { List list; for (int i = 1; i <= n; i++) { list.insert(i); } Node* curr = list.getHead(); while (n > 1) { for (int i = 1; i < m; i++) { curr = list.getNext(curr); } Node* temp = curr; curr = list.getNext(curr); list.remove(temp); n--; } return list.getHead()->data; } int main() { int n = 5; int m = 3; int result = josephus(n, m); std::cout << "The last person is " << result << std::endl; return 0; } ``` 这个程序将输出: ``` The last person is 4 ``` 这就是使用C++单向循环链表解决约瑟夫问题的一个简单的例子。希望能对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值