Comic: Funny Egg Throwing Problem
Click on "Programmer Xiaohui" above and choose to follow the official account for
interesting and meaningful articles to be delivered as soon as possible!
----- the next day-----
Topic: Throwing eggs problem
There are 2 eggs, thrown down from the 100th floor to test the hardness of the eggs. For example, if the egg is not broken on the 9th layer but broken on the 10th layer, the critical point at which the egg will not break is the 9th layer.
Question: How to use the least number of attempts to test the critical point where the egg will not break?
For example, what is the dumbest test method?
Throw one of the eggs down from the first layer.
If it is not broken on the first layer, change to the second layer and throw
it. If it is not broken on the second layer, switch to the third layer and throw it
.......
If the 59th layer is not broken, switch to the 60th layer and throw
it. The 60th floor is broken, indicating that the critical point for not breaking is the 59th floor
In the worst case, this method requires 100 throws.
Method 1: Dichotomy
Using a method similar to binary search, throw the eggs down from half the floor (50th floor).
If the first egg is broken in the 50th layer, the second egg will be thrown from the first layer, growing layer by layer, until the 49th layer.
If the first egg is not broken on the 50th floor, continue to use the dichotomy and throw it down on half of the remaining floors (75th floor)...
In the worst case, this method needs to be tried 50 times.
Method 2: Square Root Method
How to make the number of attempts of the first egg and the second egg as balanced as possible?
It's very simple, do a square root operation, the square root of 100 is 10.
Therefore, we try to throw once every 10 floors, the first time from the 10th floor, the second time from the 20th floor, the third time from the 30th floor...all the way to the 100th floor.
The best case like this is broken on the 10th floor, and the number of attempts is 1 + 9 = 10 times.
The worst case is broken on the 100th floor, the number of attempts is 10 + 9 = 19 times.
However, there is a small optimization point here. We can start throwing from the 15th floor, and then from the 25th and 35th floors... all the way to the 95th floor.
In this case, the worst case is broken on the 95th floor, and the number of attempts is 9 + 9 = 18 times.
————————————
Assuming that the optimal number of attempts is x times, why should the xth layer be selected for the first throw?
The explanation here will be a bit brain-burning, so please sit down and take care of it:
Suppose the first time is thrown on the x+1th layer:
if the first egg is broken, then the second egg can only be thrown layer by layer from the first layer, until the xth layer.
In this way, we tried x+1 times in total, which is contrary to the assumption that we tried x times. It can be seen that the floor thrown for the first time must be less than the x+1 floor.
Suppose the first time is thrown on the x-1th layer:
if the first egg is broken, then the second egg can only be thrown layer by layer from the first layer, and continues to the x-2th layer.
In this way, we tried x-2+1 = x-1 times in total. Although it did not exceed the number of assumptions, it seemed a bit too conservative.
Suppose the first time is thrown at the xth layer:
if the first egg is broken, then the second egg can only be thrown layer by layer from the first layer, and continues to the x-1 layer.
In this way, we tried x-1+1 = x times in total, which just happened to not exceed the number of assumptions.
Therefore, if you want to make the floor span as large as possible and ensure that the number of attempts x is not exceeded, then the best choice for the first egg throw is the xth floor.
Method 3: Solving equation method
x + (x-1) + (x-2) + ... + 1 = 100
This equation is not difficult to understand:
The polynomial on the left is the sum of the floor span of each egg throw. Since it is assumed to try x times, this polynomial has x terms in total.
On the right is the total number of floors 100.
Let's solve this equation below:
x + (x-1) + (x-2) + ... + 1 = 100 is converted to
(x+1)*x/2 = 100
Finally x is rounded up to get x = 14
Therefore, the number of attempts for the optimal solution in the worst case is 14 times, and the floor where the first egg is thrown is also 14 floors.
Finally, let us enumerate the number of floors tried when the first egg is not broken:
14,27, 39, 50, 60, 69, 77, 84, 90, 95, 99, 100
Give a chestnut verification:
If the critical point at which the egg does not break is the 65th floor, then the floor where the first egg is thrown is 14, 27, 50, 60, 69. It broke with a snap at this time.
The second egg continued, starting from layer 61, 61, 62, 63, 64, 65, 66, and broke with a snap.
Therefore, 65 layers of critical points that will not be broken are obtained, and the total number of attempts is 6 + 6 = 12 <14.
A few additions:
1. The next issue of Xiaohui will explain how to use dynamic programming to find a general solution to the problem of throwing eggs. Friends who don't know much about dynamic programming can read Xiaohui's previous comic preview:
Comic: What is dynamic programming? (Integrated version)
2. This cartoon is pure entertainment, please cherish your current work as much as possible, and do not imitate Xiao Hui's behavior.
—————END—————
Friends who like this article, please press and hold the picture to follow the subscription account programmer Xiaohui, and watch more exciting content