首页 > 其他分享 >数据结构-小孩出圈问题(约瑟夫环问题)

数据结构-小孩出圈问题(约瑟夫环问题)

时间:2023-02-03 21:55:05浏览次数:38  
标签:getNext Boy temp int 约瑟夫 问题 数据结构 public first

假设有m个小孩,数到n的小孩出列,直到全部出去为止。

使用环形链表解决约瑟夫环问题。

package com.linkedlist;

public class JosephuLinkeslist {
    public static void main(String[] args) {

        Josephu j=new Josephu();

        j.output(5,2,1);
    }
}
class Josephu{
    private Boy first=null;
    public  void add(int num){
        Boy temp=null;
        for (int i = 1; i <= num; i++) {
            Boy boy =new Boy(i);
            if( i == 1){
                first = boy;
                first.setNext(first);
                temp = boy;
            }else{
                temp.setNext(boy);
                boy.setNext(first);
                temp = boy;
            }
        }
    }
    public void show(){
        if(first == null){
            return;
        }
        Boy temp = first;
        while(true){
            System.out.println(temp.getI());
            temp = temp.getNext();
            if(temp == first){
                break;
            }
        }
    }

    /**
     *
     * @param m 表示多少个小孩
     * @param n 表示数几次出圈
     * @param start 从第几个开始数
     */
    public void output(int m,int n,int start){
        add(m);
        if(first == null || start < 1 || start > m) {
            return;
        }
        Boy temp = first;
        while(true){
            if(temp.getNext() == first){
                break;
            }
            temp = temp.getNext();
        }
        for (int i = 0; i < start -1; i++) {
            first = first.getNext();
            temp = temp.getNext();
        }
        while(true){
            if(temp == first){
                break;
            }
            for (int i = 0; i < n-1; i++) {
                first = first.getNext();
                temp = temp.getNext();
            }
            System.out.println(first .getI());
            first = first.getNext();
            temp.setNext(first);
        }
        System.out.println("最后:"+temp.getI());

    }

}
class Boy{
    private int i;
    private Boy next;

    public int getI() {
        return i;
    }

    public void setI(int i) {
        this.i = i;
    }

    public Boy getNext() {
        return next;
    }

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

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

 

标签:getNext,Boy,temp,int,约瑟夫,问题,数据结构,public,first
From: https://www.cnblogs.com/xming2023/p/17090535.html

相关文章

  • mybatis数据不匹配问题
    错误:数据不匹配的问题原因:在初始化数据时。创建构造参数时只创建了全参,没有创建口参。具体原因:因为mybatis框架会调用这个默认构造方法来构造实例对象。即Class类的new......
  • win10专业版64位自动登录问题
    试一下直接使用注册表修改:1、在运行中执行regedit打开注册表;2、浏览到如下路径:HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\WindowsNT\CurrentVersion\Winlogon3、在右......
  • 前后端分离,后端处理跨域问题
    由于前后端的端口和地址都有可能不一样,而我这边主要是后端维护,因此在后端servlet里面设置后端响应:publicclassBaseServletextendsHttpServlet{@Overridepr......
  • Cesium 在antd pro 框架下使用问题
    刚开始也是按官网的操作,报Youmayneedanappropriateloadertohandlethisfiletype,currentlynoloadersareconfiguredtoprocessthisfile错误,然后试图配置co......
  • IIS WordPress 单站点,多站点 中文URL乱码和重定向多次问题解决方法
    前提是需要安装IISURL重写组件(安装方法这里不说明,搜一下资料就有)1、站点网站根目录新增一个chineseurl.php文件用来处理中文url问题<?php//IISMod-Rewriteif(is......
  • 存储设备告警,如何秒速定位问题
    对运维来说,保证业务系统的稳定、可用、安全是工作核心。盯系统、服务器或模块组件,查看日志、调整参数、性能调优、配置更改、响应需求等工作都是围绕这个目标而进行。随着企......
  • [spring security]错误使用spring security内置加密工具BCryptPasswordEncoder引发的
    参考博文实战代码(十):SpringbootRestTemplate连接池记一次使用BCryptPasswordEncoder,设置了不合理参数导致耗时严重的坑高安全加密BCrypt及其性能缺陷背景项目上线前......
  • 数据结构-基础
    1.什么是数据结构?数据结构是计算机存储、组织数据的方式。数据结构是指数据之间存在一种换种多种特定关系的数据元素的集合。结构包括物理结构和逻辑结构。数据逻辑结构......
  • Redis数据结构
    1.SDSstructsdshdr{//记录buf数组中已使用字节的数量//等于SDS所保存字符串的长度intlen;//记录buf数组中未使用字节的数量intfree;/......
  • 一次JSF上线问题引发的MsgPack深入理解,保证对你有收获
    作者:京东零售肖梦圆前序某一日晚上上线,测试同学在回归项目黄金流程时,有一个工单项目接口报JSF序列化错误,马上升级对应的client包版本,编译部署后错误消失。线上问题是解决......