问题: 15个基督徒和15个非基督徒围成一圈,从任意位置依次报数,数到9就杀了这个人,然后重新开始计数,直到只剩15个人
def main():
persons = [True] * 30
counter = 0
number = 0
index = 0
while counter < 15:
if persons[index]:
number += 1
if number == 9:
persons[index] = False
counter += 1
number = 0
index += 1
index %= len(persons) # 下标取到超过最后一位变为0
for index, person in enumerate(persons):
# enumerate枚举:同时获得下标和元素;高级用法
print(f'{index}:{"基" if person else "非"}')
if __name__ == '__main__':
main()
网友评论