当前位置: 首页 > news >正文

python入门编程题

python入门编程题

"""
30个人围坐在一圈,每个人坐一个凳子,现在从第一个人开始数人,
每次数到9(即每次都需要数够9个人),然后就把数到的人踢出局(人离开,凳子留下),
如果踢出局的人数满15,则终止游戏。

要求:
打印出所有踢出局的人的顺序号(所有人的顺序号从1开始到30结束)
"""



点击查看代码
ren_30 = [1]*30                   # [1, 1, 1, 0, 1, 1, ..., 1]
current = 0
c_count = 0while ren_30.count(0) < 15:current = current % 30if ren_30[current] == 0: # 跳过该人,之前已被踢出局, 不计数current += 1        continueelse: c_count += 1         # 该凳子上有人,参与计数if c_count == 9:c_count = 0ren_30[current] = 0     # 踢出该人print(f"{current+1}")current += 1


点击查看代码
people = {}
for i in range(1, 31):  # range(30)people[i] = 1j = 0  # 记录发下了多少人
check_total = 0  # 记录数到9, 9个人必须是没下船的
while True:if j == 15:breakfor i in range(1, 31):if people[i] == 1:check_total += 1else:continueif check_total == 9:people[i] = 0check_total = 0j += 1print(i)