免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: yingweixu
打印 上一主题 下一主题

Python 练习题 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-05-14 19:36 |显示全部楼层 |倒序浏览
练习题:有一百个人,分别从1一直到100。现在有人拿枪从第一个开始枪毙,每枪毙一个跳过一个,一直到一轮完成。接着在活着的人里面再次枪毙第一个,间隔一个再枪毙一个,请问最后活着的是这一百个人里的第几个人?看谁写的简单。

论坛徽章:
0
2 [报告]
发表于 2013-05-14 23:22 |显示全部楼层
自己抛个转:

#!/usr/bin/python

people_list = []
dead_man = 0
shoot = 'no'

for i in range(0,100):
    people_list.append("alive")

while dead_man < 100:
    for point in range(0,100):
        if people_list[point] == 'alive' and shoot == 'yes':
            people_list[point] = 'dead'
            dead_man += 1
            shoot = 'no'
            if dead_man == 100:
                print point+1
        elif people_list[point] == 'alive' and shoot == 'no':
            shoot = 'yes'

论坛徽章:
0
3 [报告]
发表于 2013-05-15 11:03 |显示全部楼层
回复 3# hellioncu

你的答案和我的答案不一样~ 你的结果是 63, 我的是 73.

   

论坛徽章:
0
4 [报告]
发表于 2013-05-15 15:08 |显示全部楼层
回复 9# V_客

第一个就开枪的话,我的答案是72.
   

论坛徽章:
0
5 [报告]
发表于 2013-05-16 14:42 |显示全部楼层
回复 13# GhostFromHeaven


    64 是对的,我之前少了一个环节,每轮开始的时候,都要重新设置:第一个开枪。

#!/usr/bin/python

people_list = []
dead_man = 0

for i in range(0,100):
    people_list.append("alive")

while dead_man < 100:
    shoot = 'yes'
    for point in range(0,100):
        if people_list[point] == 'alive' and shoot == 'yes':
            people_list[point] = 'dead'
            dead_man += 1
            shoot = 'no'
            if dead_man == 100:
                print point+1
        elif people_list[point] == 'alive' and shoot == 'no':
            shoot = 'yes'
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP