1 #题目: A ball from the height of 100 meters free fall, each landing after the back to the original height of half, and then fall, the 10th time to landing, how many meters? How high is the 10th time rebound?
Code:
2 3 h = 0 4 li = [] 5 for I in Range (1,11): 6 s = 100/(2** (i-1)) 7 li.append (s) 8 9 for x in Li:10 H + = x print (LI) print (' The ball is on the tenth time, it has been%.10f m, the 10th bounce height is%.10f '% ((h*2-100), LI[-1]/2)
Operation Result:
[[email protected] code_100]# python code_20.py [100.0, 50.0, 25.0, 12.5, 6.25, 3.125, 1.5625, 0.78125, 0.390625, 0.195312 5] When the ball landed on the tenth time, it was a total of 299.6093750000 meters, and the 10th height of the rebound was 0.0976562500[[email protected] code_100]#
Code Explanation:
2 3 h = 0 #初始化总高度为0 4 li = [] # Initialize the height of each landing the list is empty 5 for i in range (1,11): #遍历10此落地 6 s = 100/(2** ( i-1)) #s为每次落地的高度 (100,50,25,12.5 ...) 7 li.append (s) #将每次落地的高度放入列表li 8 9 for x in li: # Traverse this height list 10 h += x #将每次落地的高度累加 11 print (LI) #打印以便验证这个高度list 12 print (' The ball was landed on the tenth time, a total of%.10f meters, 10th this rebound height is%.10f '% ((h*2-100), LI[-1]/2)) #总共经过的米数是列表累加的两倍减去100 (Each process consists of bouncing and landing two parts away from, #第一次落地没有之前的弹起, so minus 100), the tenth time after landing the distance, is the tenth landing distance divided by 2
This article is from the "Learning Notes" blog, so be sure to keep this source http://netsyscode.blog.51cto.com/6965131/1747625
"Python" programming language Introduction Classic 100 cases--20