Python 练习实例20

3842 1
LZH实名认证 2022-3-16 22:50:13 | 显示全部楼层 |阅读模式
题目:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10次落地时,共经过多少米?第10次反弹多高?

程序分析:无

程序源代码:

Python 实例
#!/usr/bin/python
# -*- coding: UTF-8 -*-

tour = []
height = []

hei = 100.0 # 起始高度
tim = 10 # 次数

for i in range(1, tim + 1):
    # 从第二次开始,落地时的距离应该是反弹高度乘以2(弹到最高点再落下)
    if i == 1:
        tour.append(hei)
    else:
        tour.append(2*hei)
    hei /= 2
    height.append(hei)

print('总高度:tour = {0}'.format(sum(tour)))
print('第10次反弹高度:height = {0}'.format(height[-1]))
以上实例输出结果为:

总高度:tour = 299.609375
第10次反弹高度:height = 0.09765625
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2026 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行