大家好,又见面了,我是你们的朋友全栈君。
#!/usr/bin/python
# -*- coding:utf-8 -*-
def initList(n):
list = []
for i in range(n):
list.append(i+1)
return list
def run_josephus(list, m):
n = len(list)
i = 0
j = 0
while n > 1:
n = len(list)
# print("i=",i,",j=",j)
# print("list=",list)
if i == m-1:
print(list[j])
list.remove(list[j])
i = 0
n = len(list)
if j == n:
j = 0
continue
else:
i = i+1
if j == n-1:
j = 0
else:
j = j+1
print(list[0])
m = 3
for n in range(11):
list = initList(n+1)
print(list)
run_josephus(list, m)
'''
n = 3
list = initList(n)
print(list)
run_josephus(list, m)
'''
结果:
[1]
1
[1, 2]
1
2
[1, 2, 3]
3
1
2
[1, 2, 3, 4]
3
2
4
1
[1, 2, 3, 4, 5]
3
1
5
2
4
[1, 2, 3, 4, 5, 6]
3
6
4
2
5
1
[1, 2, 3, 4, 5, 6, 7]
3
6
2
7
5
1
4
[1, 2, 3, 4, 5, 6, 7, 8]
3
6
1
5
2
8
4
7
[1, 2, 3, 4, 5, 6, 7, 8, 9]
3
6
9
4
8
5
2
7
1
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
3
6
9
2
7
1
8
5
10
4
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
3
6
9
1
5
10
4
11
8
2
7
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/136125.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...