从1到30的数组,从5开始报数,报到3倍数去除,指导剩下两个
题目:30个小朋友围坐一圈(编号1~30),第1号小朋友从5开始报数,报到3的倍数的小朋友离开圈子,其他小朋友继续,最后剩下两个小朋友游戏结束,最后两个小朋友的位置编号是多少,写出代码
我的解题
def clear(list_max: int, start: int, fux: int):
from copy import deepcopy
list1 = [i for i in range(1, list_max + 1)]
list2 = deepcopy(list1)
count = 1
start -= 1
while len(list1) != 2:
for e, i in enumerate(list1):
if len(list2) == 2:
break
if (e + 1 + start) % fux == 0:
list2.remove(i)
if len(list2) < 2:
break
start += len(list1)
count += 1
list1 = deepcopy(list2)
return list2
if __name__ == "__main__":
print(clear(30, 5, 3))
大佬的解题
def func(start, fux):
list1 = [{'index': _, 'flag': True} for _ in range(1, 31)]
length = len(list1)
while True:
for _ in list1:
if not _['flag']:
continue
if length == 2:
return list1
if start % fux == 0:
_['flag'] = False
length -= 1
start += 1