Python 实现【报数游戏】

360影视 日韩动漫 2025-05-18 09:54 2

摘要:def josephus_problem(M): if M = 100: return "ERROR!" people = list(range(1, 101)) # 编号1到100 index = 0 # 当前报数的人的索引 while

def josephus_problem(M): if M = 100: return "ERROR!" people = list(range(1, 101)) # 编号1到100 index = 0 # 当前报数的人的索引 while len(people) >= M: # 计算需要移除的人的索引:(当前索引 + M - 1) % 剩余人数 index = (index + M - 1) % len(people) people.pop(index) # 剩余的人按升序排列 remaining = sorted(people) return ",".join(map(str, remaining))# 示例测试M = int(input.strip)print(josephus_problem(M))

来源:飞扬教育

相关推荐