《Cracking the Coding Interview》——第6章:智力题——题目5
2014-03-20 01:14
369 查看
2014-03-20 01:08
题目:扔鸡蛋问题。有一个鸡蛋,如果从N楼扔下去恰好会摔碎,低于N楼则不碎,可以继续扔。给你两个这样的鸡蛋,要求你一定得求出N,怎么扔才能减少最坏情况下的扔的次数?
解法:为了让worst case得到最优化,就需要让best case和worst case最接近。具体做法请参见书上题解,因为我一直在想着二分,实在是摸不着头脑。
代码:
题目:扔鸡蛋问题。有一个鸡蛋,如果从N楼扔下去恰好会摔碎,低于N楼则不碎,可以继续扔。给你两个这样的鸡蛋,要求你一定得求出N,怎么扔才能减少最坏情况下的扔的次数?
解法:为了让worst case得到最优化,就需要让best case和worst case最接近。具体做法请参见书上题解,因为我一直在想着二分,实在是摸不着头脑。
代码:
// 6.5 Given 100 floors and 2 eggs. // If the egg can sustain a dropping from nth floor, and will break for higher drop, please find a way to determine n, and minimize the number of droppings. // Answer: // That's indeed a brain teaser with mathematical principle. // It just doesn't make sense to me, why it is optimized when num_of_drop(egg1) + num_of_drop(egg2) always stays the same. // Perhaps you should read the book yourself. I do hope that I don't face such a question. Quite a teaser, tease me more. int main() { return 0; }
相关文章推荐
- 《Cracking the Coding Interview》——第6章:智力题——题目3
- 《Cracking the Coding Interview》——第6章:智力题——题目4
- 《Cracking the Coding Interview》——第6章:智力题——题目6
- 《Cracking the Coding Interview》——第6章:智力题——题目1
- 《Cracking the Coding Interview》——第6章:智力题——题目2
- 《Cracking the Coding Interview》——第13章:C和C++——题目9
- 《Cracking the Coding Interview》——第14章:Java——题目5
- 《Cracking the Coding Interview》——第18章:难题——题目5
- 《Cracking the Coding Interview》——第1章:数组和字符串——题目4
- 《Cracking the Coding Interview》——第3章:栈和队列——题目2
- 《Cracking the Coding Interview》——第5章:位操作——题目3
- 《Cracking the Coding Interview》——第9章:递归和动态规划——题目4
- 《Cracking the Coding Interview》——第11章:排序和搜索——题目4
- 《Cracking the Coding Interview》——第8章:面向对象设计——题目5
- 《Cracking the Coding Interview》——第13章:C和C++——题目10
- 《Cracking the Coding Interview》——第14章:Java——题目6
- 《Cracking the Coding Interview》——第16章:线程与锁——题目2
- 《Cracking the Coding Interview》——第18章:难题——题目6
- 《Cracking the Coding Interview》——第1章:数组和字符串——题目5
- 《Cracking the Coding Interview》——第3章:栈和队列——题目3