约瑟夫问题 The Josephus Problem 非递归算法求解
来源:互联网 发布:菲律宾禁毒知乎 编辑:程序博客网 时间:2024/05/20 10:23
据说著名犹太历史学家 Josephus有过以下的故事:
在罗马人占领乔塔帕特后,39 个犹太人与Josephus及他的朋友躲到一个洞中,39个犹太人决定宁愿死也不要被敌人抓到,于是决定了一个自杀方式,41个人排成一个圆圈,由第1个人开始报数,每报数到第3人该人就必须自杀,然后再由下一个重新报数,直到所有人都自杀身亡为止。然而Josephus 和他的朋友并不想遵从。首先从一个人开始,越过k-2个人(因为第一个人已经被越过),并杀掉第k个人。接着,再越过k-1个人,并杀掉第k个人。这个过程沿着圆圈一直进行,直到最终只剩下两个人留下,这两个人就可以继续活着。问题是,一开始要站在什么地方才能避免被处决?
public class solution {public static void main(String[] args) {Data data = new Data(41);while(data.getArray().length>2){data.die();data.show();}}}
public class Data {private int array[];private int init = 2;public Data(int j) {if (j > 0) {array = new int[j];for (int i = 0; i < j; i++) {array[i] = i;}}}public int[] getArray() {return array;}public void setArray(int[] array) {this.array = array;}public int getInit() {return init;}public void setInit(int init) {this.init = init;}public void die() {int i = 0;for (; i < array.length; i++) {int y = 3 * i + init;if (y > array.length - 1) {this.init = y - array.length;break;}array[y] = 0;}int next[] = new int[array.length - i];int a = 0;for (int j = 0; j < array.length; j++) {if (array[j] != 0) {next[a] = array[j];a++;}}array = next;}public void show() {System.out.println("= START ====");for (int i = 0; i < array.length; i++) {System.out.println("array[" + i + "]:" + array[i]);}System.out.println("= END ====");}}
>>>>>>>> 下面是我使用的另一种快速的解法 >>>>>>>>>>>>>>>>>>>>
public class Another {private int array[];public int[] getArray() {return array;}public void setArray(int[] array) {this.array = array;}public Another(int j) {if (j > 0) {array = new int[j];for (int i = 0; i < j; i++) {array[i] = 1;}}}public void die() {int count = 0;int now = 0;int left = array.length;while (true) {if (array[now] == 1) {count++;}if (count == 3) {array[now] = 0;count = 0;left -= 1;}if (left == 2) {break;}now++;if (now == array.length) {now = 0;}}}public void show() {System.out.println("= START ====");for (int i = 0; i < array.length; i++) {if(array[i]==1){System.out.println("array[" + i + "]:" + array[i]);}}System.out.println("= END ====");}public static void main(String[] args) {Another data = new Another(41);data.die();data.show();}}
Josephus要他的朋友先假装遵从,他将朋友与自己安排在第16个与第31个位置,于是逃过了这场死亡游戏
0 0
- 约瑟夫问题 The Josephus Problem 非递归算法求解
- 约瑟夫问题(Josephus problem)
- Josephus problem(约瑟夫问题)
- 约瑟夫问题Josephus problem
- 约瑟夫问题(Josephus problem)
- 约瑟夫问题(Josephus Problem)
- 约瑟夫问题(Josephus problem)
- 约瑟夫问题(Josephus Problem)算法分析及代码
- 约瑟夫问题递归求解
- 约瑟夫问题(Josephus problem)的一点思考
- lightoj 1179 - Josephus Problem 约瑟夫问题
- 约瑟夫环问题——Josephus Problem
- 约瑟夫环问题(josephus problem)详解
- josephus problem——约瑟夫问题
- Algorithm Gossip: 约瑟夫问题(Josephus Problem)
- 约瑟夫(Josephus problem)环问题初探
- Josephus Problem 约瑟夫环问题的讨论
- Java-约瑟夫问题(Josephus Problem)
- 分治法实现循环赛事日程问题
- hdu4263
- Hadoop——上传本地文件到hdfs
- Java中内存,成员变量,局部变量
- 第四周 项目2-分数类雏形
- 约瑟夫问题 The Josephus Problem 非递归算法求解
- 整数划分问题
- 蓝桥杯 C本科B组 调和级数无限逼近
- (others)PPOE的疑惑
- Why do they choose the film faced plywood
- 正则整理
- 类方法--简单计算器
- 23. PHP 可变变量
- [LeetCode]First Missing Positive