from fib import fib # 参考01.线性表 def josephus_a(n, k, m): """ 约瑟夫环 没有人用0表示,n个人出列即结束 :param n: 包含n个人 :param k: 找到第k个人,从那里开始 :param m: 数m个人(在坐的),遇到表的末端就转回下标0继续,把表示第m个人的元素修改为0 :return: """ people = list(range(1, n + 1)) i = k - 1 # i表示数组下标 for num in range(n): count = 0 while count < m: if people[i] > 0: count += 1 if count == m: print(people[i], end='') people[i] = 0 i = (i + 1) % n if num < n - 1: print(", ", end='') else: print('') return def josephus_l(n, k, m): """ 约瑟夫环: 退出的人从表中删除 :param n: :param k: :param m: :return: """ people = list(range(1, n + 1)) i = k - 1 # num逐渐-1 for num in range(n, 0, -1): i = (i + m - 1) % num print(people.pop(i), end=(", " if num > 1 else " ")) return class LCList(object): pass class Josephus(fib.LCList): """基于循环单链表""" def turn(self, m): """跳转""" for i in range(m): self._rear = self._rear.next def __init__(self, n, k, m): """ :param n: 包含n个人 :param k: 找到第k个人,从那里开始 :param m: 数m个人 """ super(Josephus, self).__init__() # 添加数据1~n for i in range(1, n + 1): self.append(i) # 从k-1开始位置 self.turn(k - 1) while not self.is_empty(): self.turn(m - 1) print(self.pop(), end=(" " if self.is_empty() else ", ")) if __name__ == '__main__': josephus_a(5, 1, 2) josephus_l(5, 1, 2) Josephus(5, 1, 2)