谷歌面试题:两个玻璃球摔碎的楼层高度

更新时间:2023-06-19 21:56:23 阅读: 评论:0

⾕歌⾯试题:两个玻璃球摔碎的楼层⾼度
给你两个⼀摸⼀样的球,这两个球如果从⼀定的⾼度掉到地上有可能就会摔碎,当然,如果在这个⾼度以下往下扔,怎么都不会碎,当然超过这个⾼度肯定就⼀定摔碎了。现在已知这个恰巧摔碎⾼度范围在⼀层楼到100层楼之间。如何⽤最少的试验次数,⽤这两个玻璃球测试出摔碎的楼⾼。
两个球,⼀个⽤来粗调,⼀个⽤来精调,具体做法是这样的。
⾸先拿第⼀个球到10层楼去试,如果没有摔碎,就去20层楼,每次增加10层楼。如果在某个⼗层楼摔碎了,⽐如60层,就知道摔碎的⾼度在51到60层之间,接下来从51层开始⼀层层地试验,这样就可以保证不出⼆⼗次,⼀定能试出恰巧摔碎的⾼度。
这是从统计学的⾓度来说,最完美的策略。从⼯程学的⾓度看,这是考察⼀个⼈有没有掌握粗调和精调的⽅法,就像显微镜上有两个旋钮,第⼀个粗调,让你⼤致看到图像,第⼆个是精调,能让你看清楚图像。
stairs = int(input("Plea input the total stairs: "))
def steps(step,target):屈原的千古名句
i = j = 0
while target > 0:
target -= step
树真好散文诗
i += 1
j = target + step怎么重启电脑
return i + j
儿童故事大全>魔法气球
steps_dict = dict()
for j in range(2,int(stairs/2) + 1,1):
sum = 0.0
for i in range(1,stairs + 1,1):
伊字五行属什么sum += steps(j,i)
print("step: %d, mean steps: %f" % (j, sum/stairs))
steps_dict[j] = sum/stairs
temp_key = ""
temp_steps = stairs
for key in steps_dict.keys():
if temp_steps > steps_dict[key]:
temp_steps = steps_dict[key]
temp_key = key
print("The best step should be %d and the mean steps is %f." % (temp_key, temp_steps))
运⾏结果:
PS F:\for_project\python> python .\steps_google.py
Plea input the total stairs: 100
The best step should be 10 and the mean steps is 11.000000.
PS F:\for_project\python> python .\steps_google.py
Plea input the total stairs: 1000
凤凰的图片The best step should be 32 and the mean steps is 32.532000.
PS F:\for_project\python> python .\steps_google.py
Plea input the total stairs: 200
关于爱祖国的古诗The best step should be 14 and the mean steps is 15.050000.

本文发布于:2023-06-19 21:56:23,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/82/993864.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:摔碎   层楼   玻璃球
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图