Java环形链表--约瑟夫问题
来源:互联网 发布:斗鱼点播软件 编辑:程序博客网 时间:2024/05/17 02:53
n个小孩围成圈,丢手帕,从第start个小孩开始丢,每到第step个小孩出局;接着从下一个小孩开始,直到最后一个小孩为止,游戏结束。
/** * @author Jiaozl * CycLink cyclink = new CycLink(); * cyclink.setLen(5); * cyclink.createLink(); * cyclink.setStart(2); * cyclink.setStep(2); * cyclink.show(); * cyclink.play(); */public class CycLink { Child firstChild = null; // 指向链表第一个小孩的引用 Child temp=null; int len = 0; // 表示公有几个小孩 int start = 0; int step = 0; /** * 设置链表的大小 */ public void setLen(int len) { this.len = len; } /** * 设置从第几个人开始数数 */ public void setStart(int start) { this.start = start; } /** * 设置step */ public void setStep(int step) { this.step = step; } /** * 开始play */ public void play() { Child temp = this.firstChild; // 先找到开始数数的人 for(int i=1; i<=start-1; i++) { temp = temp.nextChild; } while(this.len!=1) { // 数m下 for (int i = 1; i < step; i++) { temp = temp.nextChild; } Child temp2 = temp; while (temp2.nextChild!=temp) { temp2 = temp2.nextChild; } // 将疏导step的小孩,退出圈圈 temp2.nextChild = temp.nextChild; temp = temp.nextChild; this.len--; } System.out.println("最后剩下的小孩:" + temp.no); } /** * 初始化环形链表 */ public void createLink() { for (int i=1; i<=len; i++) { if(i==1) { Child ch = new Child(i); this.firstChild = ch; this.temp = ch; } else { if(i==len) { Child ch = new Child(i); temp.nextChild = ch; temp = ch; temp.nextChild = this.firstChild; } else { Child ch = new Child(i); temp.nextChild = ch; temp = ch; } } } } /** * 打印环形链表 */ public void show() { Child temp = this.firstChild; do{ System.out.print(temp.no + " "); temp = temp.nextChild; } while(temp!=this.firstChild); }}class Child{ int no; Child nextChild = null; public Child(int no) { this.no = no; }}
0 0
- Java环形链表--约瑟夫问题
- 环形链表实现约瑟夫问题 java语言
- 环形链表+约瑟夫问题实现
- 环形链表,约瑟夫问题,(7)
- 链表问题(4)-- 环形单链表的约瑟夫问题
- 链表问题---环形单链表的约瑟夫问题
- 约瑟夫问题(环形链表解题)
- 使用环形链表实现约瑟夫环
- Java——环形单链表的约瑟夫问题
- 环形单链表的约瑟夫问题
- 环形单链表的约瑟夫问题
- 环形单链表的约瑟夫问题
- 环形单链表的约瑟夫问题
- 环形链表约瑟夫问题(可参考剑指offer面试题62)
- 环形链表解约瑟夫环
- 左程云_算法与数据结构 — 链表问题 — 06环形单链表的约瑟夫问题_normal&advanced
- 数据结构 环形单链表的约瑟夫问题
- 环形单链表的约瑟夫问题 Python版
- 线程池实现原理
- RTMP协议学习
- pjsip中面向对象思路
- [读书笔记]H.265/HEVC:原理,标准与实现
- SpringMVC Controller介绍及常用注解
- Java环形链表--约瑟夫问题
- JAVA_多线程
- C/C++标识符的规定
- 紧急救援
- 搭建以太坊私有链和部署智能合约开发环境
- React学习之进阶性能优化(十五)
- Codeforces Round #374 (Div. 2) D. Maxim and Array 贪心
- Android 数据清除工具类
- HDU 1143Tri Tiling&&POJ2663