From 16dad8f7b6063e1a907c540e8437269d1ff31196 Mon Sep 17 00:00:00 2001 From: lixiangrong Date: Fri, 9 Apr 2021 22:35:08 +0800 Subject: [PATCH] add jsephu --- .DS_Store | Bin 0 -> 6148 bytes .../datastructure/linkedlist/Josephu.java | 141 ++++++++++++++++++ 2 files changed, 141 insertions(+) create mode 100644 .DS_Store create mode 100644 src/main/java/io/github/ehlxr/datastructure/linkedlist/Josephu.java diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..b203a9d6688d8df36e4559c3b3be686344ef81c7 GIT binary patch literal 6148 zcmeHKyG{Z@6ukqet0V>!3M;cEl?i{a66{U*0OetT1Oh7bo7ng_R+cu}TNxjTjg4Pm zK|#QCXGWP}!IBtrZ!&Y1``B~$z|Js4q)Nj^ktjz*0S04c3)2GQIFE#-tS1jte2-4E zSgH5A{Y0iXL;+FYFDk(AZie=$No^WxPO0kk zqCT;@xly-|m(t_i)M(+q7IAs6o;KgdhS%1)oXM!i zw{ygN>olM$7@K%v|GN(!Ci8KZdwl!xX1Tq4xE(CZStn3LzX?FNSolYt;D6=Ra3it}7&Bq+?|A))Z|9+8t5(Pwof2DxRlq=;D z9?9;liHGC8HpDo=VB@^lpf15=w_|<5Tk$T2HpD`{07e#LgYdxQkARjzCQ)Fe3VZ>T CjizA$ literal 0 HcmV?d00001 diff --git a/src/main/java/io/github/ehlxr/datastructure/linkedlist/Josephu.java b/src/main/java/io/github/ehlxr/datastructure/linkedlist/Josephu.java new file mode 100644 index 0000000..2037f00 --- /dev/null +++ b/src/main/java/io/github/ehlxr/datastructure/linkedlist/Josephu.java @@ -0,0 +1,141 @@ +/* + * The MIT License (MIT) + * + * Copyright © 2020 xrv + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package io.github.ehlxr.datastructure.linkedlist; + +/** + * @author ehlxr + * @since 2021-04-09 21:14. + */ +public class Josephu { + public static void main(String[] args) { + CircleSingleLinkedList circleSingleLinkedList = new CircleSingleLinkedList(); + circleSingleLinkedList.addBoy(5); + circleSingleLinkedList.show(); + + circleSingleLinkedList.countBoy(1, 2, 5); + } +} + +class CircleSingleLinkedList { + private Boy first = null; + + public void addBoy(int nums) { + if (nums < 1) { + return; + } + Boy curBoy = null; + for (int i = 1; i <= nums; i++) { + Boy boy = new Boy(i); + if (i == 1) { + first = boy; + curBoy = first; + first.setNext(first); + } else { + curBoy.setNext(boy); + boy.setNext(first); + curBoy = boy; + } + } + } + + public void show() { + if (first == null) { + System.out.println("没有boy"); + return; + } + + Boy curBoy = first; + while (true) { + System.out.printf("boy no %d\n", curBoy.getNo()); + if (curBoy.getNext() == first) { + break; + } + + curBoy = curBoy.getNext(); + } + } + + public void countBoy(int startNo, int countNum, int size) { + if (first == null || startNo > size || startNo < 1) { + return; + } + + // 找到最后一个节点 + Boy helper = first; + while (true) { + if (helper.getNext() == first) { + break; + } + helper = helper.getNext(); + } + + // 找到开始报数的位置 + for (int i = 0; i < startNo - 1; i++) { + helper = helper.getNext(); + first = first.getNext(); + } + + while (true) { + if (helper == first) { + break; + } + + for (int i = 0; i < countNum - 1; i++) { + helper = helper.getNext(); + first = first.getNext(); + } + System.out.printf("出圈的小孩 %d\n", first.getNo()); + + first = first.getNext(); + helper.setNext(first); + } + System.out.printf("最后出圈的小孩 %d\n", first.getNo()); + } +} + +class Boy { + private int no; + private Boy next; + + 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; + } +} \ No newline at end of file