题目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))