public static int josheph(int x, int num) {
// 数组初始化全为true
boolean[] a = new boolean[x];
for (int i = 0; i < x; i++) {
a[i] = true;
}
int count = 0;
int index = 0;
int len = x;
while (len > 1) {
if (a[index] == true) {
count++;
if (count == num) {
count = 0;
a[index] = false;
len--;
}
}
index++;
if (index == x) {
index = 0;
}
}
// 遍历找到最后一个为true的元素
for (int i = 0; i < a.length; i++) {
if (a[i]) {
return i;
}
}
return -1;
}
约瑟夫环
最新推荐文章于 2018-09-10 21:43:16 发布