环形单链表实现约瑟夫环问题

package CircleLinkedList;

/**
 * 根据用户的输入
 */

public class Joseph {
    public static void main(String[] args) {
        CircleBoy circleBoy = new CircleBoy();
        circleBoy.addBoy(5);
        circleBoy.showBoy();
        System.out.println("--------------");

        circleBoy.outCircle(1,2,5);//2 4 1 5 --> 3
    }

}
// 创建一个环形的单向链表
class CircleBoy{
    // 创建一个first节点,当前没有编号
    private Boy first = null;
    // 添加小孩节点,构建成一个环形的链表
    public void addBoy(int nums){
        //nums表示小孩的个数
        if(nums < 1){
            System.out.println("数值不正确,重新输入");
            return;
        }

        Boy curBoy = null;

        for(int i = 1; i <= nums; i++){
            Boy boy = new Boy(i);
            if(i == 1){
                first = boy;
                first.setNext(first);
                curBoy = boy;
            }else{
                curBoy.setNext(boy);
                boy.setNext(first);
                curBoy = boy;
            }
        }
    }

    //遍历环形链表
    public void showBoy(){
        if(first == null){
            System.out.println("环形链表为空");
            return;
        }

        Boy curBoy = first;
        while (true){
            System.out.printf("%d ",curBoy.getNo());
            if (curBoy.getNext() == first){
                break;
            }
            curBoy = curBoy.getNext();
        }

    }

    //小孩出圈,从第k个开始报数,1报到m, 初始共有n 个人
    public void outCircle(int k, int m, int n){
        if(first == null || k < 1 || m > n){
            System.out.println("数据不合理");
            return;
        }
        Boy helper = first;//指向first的后一个
        while (helper.getNext() != first){
            helper = helper.getNext();
        }

        //从第k个开始报数,先让first,helper往前走 k - 1 步
        for(int i = 0; i < k - 1; i++){
            first = first.getNext();
            helper = helper.getNext();
        }

        //从第k个开始数,即first、helper往前走 m - 1 步,然后first 出圈-->first = first.next,helper.next = first
        while (true){
            if(helper == first){
                break;//只剩下最后一个男孩在圈中
            }

            for(int i = 0; i < m - 1; i++){
                first = first.getNext();
                helper = helper.getNext();
            }
            //出圈
            System.out.printf("%d出圈\n",first.getNo());
            first = first.getNext();
            helper.setNext(first);

        }

        //退出循环之后
        System.out.println("圈中最后一个孩子是"+first.getNo());
    }

}

class Boy{
    private int no;
    private Boy next;

    public Boy(int no){
        this.no = no;
    }

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

    public int getNo(){
        return no;
    }

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

    public Boy getNext(){
        return next;
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
循环单链表是一个环形链表,最后一个节点的 next 指针指向头结点。求解约瑟夫问题可以利用循环单链表模拟,具体步骤如下: 1. 构建循环单链表,将每个人的编号作为节点的数据域,将每个节点的 next 指针指向下一个节点。 2. 定义一个计数器 count,用于记录当前数到了第几个节点。 3. 从头节点开始遍历链表,每次计数器加1,直到计数器的值等于 m 时,就找到了要删除的节点。将该节点从链表中删除,并将计数器 count 重置为 0。 4. 重复步骤 3,直到链表中只剩下一个节点,此时该节点即为最后一个幸存者的编号。 下面是具体的代码实现: ```c++ #include <iostream> using namespace std; struct ListNode { int val; ListNode* next; ListNode(int x) : val(x), next(nullptr) {} }; int josephus(int n, int m) { ListNode* head = new ListNode(1); ListNode* cur = head; for (int i = 2; i <= n; i++) { cur->next = new ListNode(i); cur = cur->next; } cur->next = head; // 构造循环链表 int count = 0; while (cur->next != cur) { // 链表中还有多于一个节点 count++; if (count == m) { ListNode* temp = cur->next; cur->next = temp->next; delete temp; // 释放要删除的节点 count = 0; } else { cur = cur->next; } } int ans = cur->val; delete cur; // 释放最后一个节点 return ans; } int main() { int n = 5, m = 2; int ans = josephus(n, m); cout << "The last survivor's number is: " << ans << endl; return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值