python入门编程题
"""
30个人围坐在一圈,每个人坐一个凳子,现在从第一个人开始数人,
每次数到9(即每次都需要数够9个人),然后就把数到的人踢出局(人离开,凳子留下),
如果踢出局的人数满15,则终止游戏。
要求:
打印出所有踢出局的人的顺序号(所有人的顺序号从1开始到30结束)
"""
答案:
"""
9
18
27
6
16
26
7
19
30
12
24
8
22
5
23
"""
点击查看代码
ren_30 = list(range(1, 31)) # 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
remove_list = [] # 0, 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
current = 0
while len(remove_list) < 15:
current = (current + 8) % len(ren_30)
people_index = ren_30.pop(current)
print(f"{people_index}")
remove_list.append(people_index)
点击查看代码
ren_30 = [1]*30 # [1, 1, 1, 0, 1, 1, ..., 1]
current = 0
c_count = 0
while ren_30.count(0) < 15:
current = current % 30
if ren_30[current] == 0: # 跳过该人,之前已被踢出局, 不计数
current += 1
continue
else:
c_count += 1 # 该凳子上有人,参与计数
if c_count == 9:
c_count = 0
ren_30[current] = 0 # 踢出该人
print(f"{current+1}")
current += 1
点击查看代码
people = {}
for i in range(1, 31): # range(30)
people[i] = 1
j = 0 # 记录发下了多少人
check_total = 0 # 记录数到9, 9个人必须是没下船的
while True:
if j == 15:
break
for i in range(1, 31):
if people[i] == 1:
check_total += 1
else:
continue
if check_total == 9:
people[i] = 0
check_total = 0
j += 1
print(i)
本博客是博主个人学习时的一些记录,不保证是为原创,个别文章加入了转载的源地址,还有个别文章是汇总网上多份资料所成,在这之中也必有疏漏未加标注处,如有侵权请与博主联系。
如果未特殊标注则为原创,遵循 CC 4.0 BY-SA 版权协议。
posted on 2025-11-11 13:26 Angry_Panda 阅读(2) 评论(0) 收藏 举报
浙公网安备 33010602011771号