Python 练习实例20

题目20:
一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10次落地时,共经过多少米?第10次反弹多高?
【话说,也有可能,我读题都错了】
闲着蛋疼,在弄出来一个麻烦的方法上,改成了一种版本三种形式,最喜欢第三个。其实两个def最好,但是对于我这种没有编程概念的人来说,喜欢把东西扔一起。希望以后改正:

#1
#high = int(input(u'原始高度int:'))#小球初始高度
#n = int(input(u'落地次数int:'))#小球落地次数
high = 100
n = 10
high_all = 0 #第n次落地时走过的长度
high_each = 0 #每次落地的高度
def ball_lands(n):
    global high_all, high_each, high
    #小球第一次落地时
    if n == 1:
        high_each = high
        high_all += high_each
        #print('1', high_each, high_all)
        print(u'第%d次小球弹起的高度为:%.2f' % (n, high_each/2))
        return high_each
    #从小球第n次落地往前推
    high_each = high * (1 / 2) ** (n - 1 )
    print(u'第%d次小球弹起的高度为:%.2f' %(n, high_each/2))
    high_all += high_each * 2
    #print('2', high_each, high_all)
    ball_lands(n - 1)
    #return high_all

ball_lands(n)
print(u'小球落地%d次,共经过%.2f米。' % (n,high_all))
#2
#high = int(input(u'原始高度int:'))#小球初始高度
#n = int(input(u'落地次数int:'))#小球落地次数
high = 100
n = 10
high_all = 0 #第n次落地时走过的长度
high_each = 0 #每次落地的高度
def ball_lands(n):
    global high_all, high_each, high
    #小球第一次落地时
    if n == 1:
        high_each = high
        high_all += high_each
        #print('1', high_each, high_all)
        #print(u'第%d次小球弹起的高度为:%.2f' % (n-1, high_each))
        return high_each
    #从小球第n次落地往前推
    high_each = high * (1 / 2) ** (n - 1 )
    print(u'第%d次小球弹起的高度为:%.2f' %(n-1, high_each))
    high_all += high_each * 2
    #print('2', high_each, high_all)
    ball_lands(n - 1)
    #return high_all
x = n + 1
ball_lands(x)
print(u'小球落地%d次,共经过%.2f米。' % (n,high_all))
#3
#high = int(input(u'原始高度int:'))#小球初始高度
#n = int(input(u'落地次数int:'))#小球落地次数
high = 100
n = 10
high_all = 0 #第n次落地时走过的长度
high_each = 0 #每次落地的高度
def ball_lands(n):
    global high_all, high_each, high
    #小球第一次落地时
    if n == 0:
        high_each = high
        high_all += high_each
        #print('1', high_each, high_all)
        return high_each
    #从小球第n次落地往前推
    high_each = high * (1 / 2) ** n
    print(u'第%d次小球弹起的高度为:%.2f' %(n, high_each))
    high_all += high_each * 2
    #print('2', high_each, high_all)
    ball_lands(n - 1)

ball_lands(n)
print(u'小球落地%d次,共经过%.2f米。' % (n,high_all))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值