当前位置:网站首页>链表应用----约瑟夫问题
链表应用----约瑟夫问题
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;
}
}
边栏推荐
- Introduction to 3 d games beginners essential 】 【 modeling knowledge
- 端口探测详解
- What is the upstream bandwidth and downstream bandwidth of the server?
- Linux服务器安装Redis,详细步骤。
- 【CNN】刷SOTA的trick
- 测试/开发程序员值这么多钱么?“我“不会愿赌服输......
- 「POJ 3666」Making the Grade 题解(两种做法)
- spark学习笔记(九)——sparkSQL核心编程-DataFrame/DataSet/DF、DS、RDD三者之间的转换关系
- keepalived:故障检测自动修复脚本
- 【毕业设计】基于Stm32的智能疫情防控门禁系统 - 单片机 嵌入式 物联网
猜你喜欢
云渲染的应用正在扩大,越来越多的行业需要可视化服务
基于TCP的聊天系统
laya打包发布apk
[Natural Language Processing] [Vector Representation] PairSupCon: Pairwise Supervised Contrastive Learning for Sentence Representation
主动信息收集
QoS Quality of Service Eight Congestion Avoidance
2020 ICPC Shanghai Site G
【毕业设计】基于Stm32的智能疫情防控门禁系统 - 单片机 嵌入式 物联网
[教你做小游戏] 斗地主的手牌,如何布局?看25万粉游戏区UP主怎么说
2022杭电多校七 Black Magic (签到)
随机推荐
TDD、FDD是什么意思?
leetcode 84.柱状图中最大的矩形 单调栈应用
《分布式微服务电商》专题(一)-项目简介
cordova 安装错误 Command failed: powershell 解决方法
西安凯新(CAS:2408831-65-0)Biotin-PEG4-Acrylamide 特性
QoS Quality of Service Seven Switch Congestion Management
手把手教你Charles抓包工具使用
QoS Quality of Service Eight Congestion Avoidance
UnitTest中的Path must be within the project 问题
Tf铁蛋白颗粒包载顺铂/奥沙利铂/阿霉素/甲氨蝶呤MTX/紫杉醇PTX等药物
第14章_MySQL事务日志
QoS服务质量八拥塞避免
Pt/CeO2单原子纳米酶|[email protected] NPs纳米酶|碳纳米管负载铂颗粒纳米酶|白血病拮抗多肽修饰的FeOPtPEG复合纳米酶
LeetCode·26.删除有序数组中的重复项·双指针
铁蛋白颗粒负载雷替曲塞/培美曲塞/磺胺地索辛/金刚烷(科研试剂)
leetcode 547.省份数量 并查集
[Go WebSocket] Your first Go WebSocket server: echo server
【知识分享】在音视频开发领域中SEI到底是个啥?
【自然语言处理】【向量表示】PairSupCon:用于句子表示的成对监督对比学习
服务器上行带宽和下行带宽指的是什么