|
发表于 2017-3-29 17:12:01
|
显示全部楼层
- ## 打开为True, 关闭为False
- ## 求第n轮后box的状态
- def box_status(n):
- box = []
- if n == 1:
- box = [True] * 100
- if n > 1:
- box = box_status(n-1)
- # 从第n个箱子开始(即box[n-1]开始,每隔n个箱子转换状态。
- for i in range(n - 1, 100, n):
- box[i] = not box[i]
- return box
- box = box_status(100)
- opened_box = 0
- for i in range(100):
- if box[i]:
- print('box-',i+1,'is open.')
- opened_box += 1
- print('Totally %d boxed are open' % opened_box)
复制代码
>>>
box- 1 is open.
box- 4 is open.
box- 9 is open.
box- 16 is open.
box- 25 is open.
box- 36 is open.
box- 49 is open.
box- 64 is open.
box- 81 is open.
box- 100 is open.
Totally 10 boxed are open |
|