当前位置:网站首页>链表应用----约瑟夫问题
链表应用----约瑟夫问题
2022-08-10 19:06:00 【幼儿园里的山大王】
一、题目

二、思路分析
可以看做是两步:创建环形链表和出圈
三、代码实现
public class Josepfu {
public static void main(String[] args) {
CircleSingleLinkedList list = new CircleSingleLinkedList();
list.add(5);
// list.show();
list.countBoy(1,2,5);
}
}
class CircleSingleLinkedList{
// 创建一个first节点,当前没有编号
private Boy first = null;
public void add(int nums){
if (nums < 0){
throw new RuntimeException("输入有误");
}
Boy temp = null;
for (int i=1;i<=nums;i++){
Boy boy = new Boy(i);
if (i == 1){
first = boy;
first.setNext(first);
temp=first;
}else{
temp.setNext(boy);
temp = boy;
boy.setNext(first);
}
}
}
public void show(){
if (Objects.isNull(first)){
throw new RuntimeException("连表为空");
}
Boy boy = first;
while (true){
System.out.println("小孩"+boy.getNo());
if (boy.getNext() == first){
break;
}
boy = boy.getNext();
}
}
public void countBoy(int start,int count,int nums){
if (start <1 || Objects.isNull(first) || start>nums){
throw new RuntimeException("输入有误");
}
// 1.创建一个辅助指针
Boy help = first;
// 2.让help 指向最后一个节点
while (true){
if (help.getNext() == first){
break;
}
help = help.getNext();
}
// 3.小孩报数前让first与help 节点向前移动start -1个位置,如果start 为1的话就不用移动,原因在于first本来就在第一位,而help本来就在最后以为
// help一直都是在first的后一位
for (int i =0;i<start-1;i++){
first=first.getNext();
help = help.getNext();
}
// 4.出圈
while (true){
// 4.1 help一直都是在first后面,当help等于first时,说明只有一个节点
if (help == first){
break;
}
/** 4.2 将两个指针移到指定位置;比如说:原本first是在第一个节点,help是在first后面,也就是最后一个节点。
现在要从第二个节点开始。那么first和help就要往前移动一位;
*/
for (int j = 0;j<count-1;j++){
first = first.getNext();
help = help.getNext();
}
System.out.println("出圈小孩"+first.getNo());
// 4.3 此时的first是要出圈的节点,所以这个时候呀first指针要下移一位。
first =first.getNext();
// 4.4 然后help连接first,也就是出圈节点的下一位。这个时候出圈节点就没有指针和连接指向他了,垃圾回收器就回对他进行回收
help.setNext(first);
}
// 5.打印最后一个节点
System.out.println("最后一个小孩"+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;
}
}
边栏推荐
- 一维数组动态和问题答记
- servlet映射路径匹配解析
- CAS:190598-55-1_Biotin sulfo-N-hydroxysuccinimide ester生物素化试
- 巧用RoaringBitMap处理海量数据内存diff问题
- 含有PEG 间隔基和一个末端伯胺基团(CAS:1006592-62-6)化学试剂
- mysql----group by、where以及聚合函数需要注意事项
- 子域名收集&Google搜索引擎语法
- 2020 ICPC Shanghai Site G
- 转铁蛋白(TF)修饰紫杉醇(PTX)脂质体(TF-PTX-LP)|转铁蛋白(Tf)修饰姜黄素脂质体
- 史上最全GIS相关软件(CAD、FME、Arcgis、ArcgisPro)
猜你喜欢

QoS服务质量七交换机拥塞管理
![[Natural Language Processing] [Vector Representation] PairSupCon: Pairwise Supervised Contrastive Learning for Sentence Representation](/img/e3/a1adb56678e9d26e09ff30be781e2a.png)
[Natural Language Processing] [Vector Representation] PairSupCon: Pairwise Supervised Contrastive Learning for Sentence Representation

史上最全GIS相关软件(CAD、FME、Arcgis、ArcgisPro)

QoS Quality of Service Six Router Congestion Management
[email protected]纳米模拟酶|PtCo合金纳米粒子"/>水溶性合金量子点纳米酶|CuMoS纳米酶|多孔硅基Pt(Au)纳米酶|[email protected]纳米模拟酶|PtCo合金纳米粒子

测试/开发程序员值这么多钱么?“我“不会愿赌服输......

(十二) findContours函数的hierarchy详解
[Teach you how to make a small game] Write a function with only a few lines of native JS to play sound effects, play BGM, and switch BGM

电脑为什么会蓝屏的原因

常见端口及服务
随机推荐
whois信息收集&企业备案信息
leetcode 547.省份数量 并查集
魔方电池如何“躺赢”?解锁荣威iMAX8 EV“头等舱”安全密码
[Go WebSocket] Your first Go WebSocket server: echo server
About npm/cnpm/npx/pnpm and yarn
2020 ICPC Shanghai Site G
Today's bug, click on the bug that the Windows dynamic wallpaper disappears in the win10 taskbar, and no solution has been found yet.
优化是一种习惯●出发点是'站在靠近临界'的地方
YOLOv3 SPP源码分析
『牛客|每日一题』岛屿数量
【无标题】基于Huffman和LZ77的GZIP压缩
FEMRL: A Framework for Large-Scale Privacy-Preserving Linkage of Patients’ Electronic Health Rec论文总结
We used 48h to co-create a web game: Dice Crush, to participate in international competitions
子域名收集&Google搜索引擎语法
苹果字体查找
巧用RoaringBitMap处理海量数据内存diff问题
烟雾、空气质量、温湿度…自己徒手做个环境检测设备
这7个自动化办公模版 教你玩转表格数据自动化
我们用48h,合作创造了一款Web游戏:Dice Crush,参加国际赛事
What is the upstream bandwidth and downstream bandwidth of the server?