当前位置:网站首页>链表应用----约瑟夫问题
链表应用----约瑟夫问题
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;
}
}
边栏推荐
- [Natural Language Processing] [Vector Representation] PairSupCon: Pairwise Supervised Contrastive Learning for Sentence Representation
- Biotin-PEG4-IC(TFP ester/amine/NHS Ester/azide)特性分享
- 铁蛋白颗粒Tf包载多肽/凝集素/细胞色素C/超氧化物歧化酶/多柔比星(定制服务)
- 云渲染的应用正在扩大,越来越多的行业需要可视化服务
- QoS服务质量八拥塞避免
- Win11连接投影仪没反应怎么解决?
- Random函数用法
- Leetcode 200.岛屿数量 BFS
- Site Architecture Detection & Chrome Plugin for Information Gathering
- 不止跑路,拯救误操作rm -rf /*的小伙儿
猜你喜欢
随机推荐
电脑重装系统Win11格式化硬盘的详细方法
laya打包发布apk
3D Game Modeling Learning Route
这7个自动化办公模版 教你玩转表格数据自动化
QoS Quality of Service Eight Congestion Avoidance
怎么完全卸载赛门铁克_Symantec卸载方法,赛门铁克卸载「建议收藏」
CAS:190598-55-1_Biotin sulfo-N-hydroxysuccinimide ester生物素化试
(十二) findContours函数的hierarchy详解
[教你做小游戏] 斗地主的手牌,如何布局?看25万粉游戏区UP主怎么说
Redis persistence mechanism
大家要的Biotin-PEG3-Br/acid/NHS ester/alcohol/amine合集分享
What is the upstream bandwidth and downstream bandwidth of the server?
MATLAB设计,FPGA实现,联合ISE和Modelsim仿真的FIR滤波器设计
【初学必备】3d游戏建模入门基础知识
转铁蛋白修饰蛇床子素长循环脂质体/负载三七皂苷R1的PEG-PLGA纳米粒([email protected] NPs)
七月券商金工精选
FEMRL: A Framework for Large-Scale Privacy-Preserving Linkage of Patients’ Electronic Health Rec Paper Summary
运维面试题(每日一题)
UnitTest中的Path must be within the project 问题
When selecting a data destination when creating an offline synchronization node - an error is reported in the table, the database type is adb pg, what should I do?