Google扔玻璃球⾯试题
今天得到app的⼀⽚⽂章中提到了⼀些⾯试题,其中有⼀Google⾯试题如下:
给你⼀个⼀摸⼀样的球,这两个球如果从⼀定的⾼度掉到地上有可能就会摔碎,当然,如果在这个⾼度以下往下扔,怎么都不会碎,当然超过这个⾼度肯定就⼀
⽂中给的答案是:金融品牌
两个球⼀个⽤来做粗调,⼀个⽤来做精调。⾸先哪⼀个球到10层楼去试,如果没有摔碎,就去20层楼,每次增加10层楼。如果在某个⼗层摔碎了,⽐如60层,这就知道那该如何确定粗调的step呢?于是写了⼀段代码,试验下如何选取合适的step,使得平均试验次数最少。
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.