forked from yidao620c/core-algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathat303_circle_queue.py
More file actions
35 lines (33 loc) · 1.04 KB
/
at303_circle_queue.py
File metadata and controls
35 lines (33 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/usr/bin/env python
# -*- encoding: utf-8 -*-
# Josephus排列
"""
Topic: n个人围成一圈,从某个指定人开始,沿着环将遇到的每第m个人移出去。
每个人移出去后,继续沿着环将剩下的人按同样规则移出去
默认队列第一个编号为1,以此类推。。。
Desc :
"""
__author__ = 'Xiong Neng'
def circle_out(n, m):
# 初始化数组,1表示在队列中,0表示已经出了队列
queue_status = [1 for n in range(0, n)]
result = [] # 出队序列
out_count = 0 # 出队人数
pass_num = 0 # 每次小循环经过的人数
index = 0 # 循环下标
while out_count < n:
while True:
if queue_status[index] == 1:
pass_num += 1
if pass_num >= m:
break
index = (index + 1) % n
# 出队
queue_status[index] = 0
out_count += 1
result.append(index + 1)
pass_num = 0
print(result)
return result
if __name__ == '__main__':
circle_out(7, 3)