2 Egg Problem
* You are given 2 eggs.
* You have access to a 100-storey building.
* Eggs can be very hard or very fragile means it may break if dropped from the first floor or may not even break if dropped from 100th floor. Both eggs are identical.
* You need to figure out the highest floor of a 100-storey building an egg can be dropped without breaking.
Now the question is how many drops you need to make. You are allowed to break 2 eggs in the process
100层楼,2个鸡蛋。某层之上扔鸡蛋就会碎。问至少要测试多少次才能试出这个层数。
答:
p1. 如果只有1个鸡蛋,那我们只能从一楼开始一层一层的测,否则砸烂了就没得测了。
现在有2个鸡蛋,有个鸡蛋可以浪费了,用一号鸡蛋来缩小范围,二号鸡蛋在一号鸡蛋缩小的范围内一层一层的测试。
比如一号鸡蛋每十层扔一下,10,20,30...... 如果在30层碎了,二号鸡蛋可以从21层开始测试。
现在的问题是如何确定一号鸡蛋每多少层测试一下,才能平衡1号2号鸡蛋的测试次数,使之最小。
设x层测试一下,最坏情况下需要测试
n =100/x+x >=2(100/x *x) ^ (1/2)
n>=20
为了使得n最小 x = 100^(1/2) = 10
扩展: 3个鸡蛋的情况
设一号鸡蛋x层测试一下,总次数
100/x+x^(1/2)
可得 x=22 最小
(数学忘的差不多了,不完整,有空再补充)