- Python- 小练习1
- 方法一
- 方法二
Python- 小练习1
如果一艘船只能载16个人,现有30个人,编号为1~30,
现在从1开始数,每数到第7个就要下船,最后输出哪些人下船
- 方法一
- 当out第一次等于30了,len值为26
member = list(range(1, 31))
print(member)
out = 6 # 下标值为out
for i in range(14):
if out <= len(member)-1:
print(member.pop(out))
out += 6
else:
out = out - len(member)
print(member.pop(out))
out += 6
- 方法二
member = list(range(1, 31))
print(member)
while len(member) > 16:
i = 1
while i < 7:
member.append(member.pop(0))
i += 1
print(member.pop(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]
7
14
21
28
5
13
22
30
9
18
27
8
19
1
Process finished with exit code 0
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)