|
发表于 2017-12-3 23:44:23
|
显示全部楼层
本帖最后由 shigure_takimi 于 2017-12-3 23:58 编辑
- # 关着为False,打开为True。
- boxes = [False for i in range(100)]
- for i in range(1,101):
- for index in range(i-1,len(boxes),i):
- boxes[index] = not boxes[index]
- print("Totally",sum(boxes),"boxes are opened!")
- for i in range(len(boxes)):
- if boxes[i]:
- print("box -", (i+1),"is opened!")
- # Totally 10 boxes are opened!
- # box - 1 is opened!
- # box - 4 is opened!
- # box - 9 is opened!
- # box - 16 is opened!
- # box - 25 is opened!
- # box - 36 is opened!
- # box - 49 is opened!
- # box - 64 is opened!
- # box - 81 is opened!
- # box - 100 is opened!
- # 和自己半年前写的相比简洁多了。
- # 放了一段时间,前两天重新拾起来,却好像反而比以前进步了。
复制代码 |
|