# An interesting interview question

Source: Internet
Author: User

I saw an interview question on the Internet a few days ago. It is quite interesting and I will study it in detail. I will post this question here to discuss with you.

A building has 100 floors. You have two identical glass beads in your hand. When you throw the glass beads down a layer, there will be two results: the glass beads are broken or not broken. This building has a critical floor. Lower than its floor, throwing glass beads down, glass beads will not be broken, equal to or higher than its floor, drop glass beads, glass beads will be broken. If the glass beads are broken, they cannot be thrown. Now, let's design a way to minimize the number of times in the worst case than in any other way. That is, design the most effective way.

For example, there is such a method: The first time you choose to throw at the 60th floor. If it is broken, it means that the critical point is at the 60th floor or below, and there is only one bead, the rest can only be from the first layer, the first layer to the experiment, the worst case, to experiment 59 times, plus the first time, a total of 60 times. If it is not broken, you only need to try it from the 61-layer, up to 40 times, plus a total of 41 times. The one with the most values in the two cases. Therefore, in the worst case, try 60 times.

So how should we design it?

After careful analysis, the key is the first choice. Suppose that at the nth layer, if it is broken when it is thrown for the first time, the second bead can only be tried from the first layer, at this point, the worst case is the N-1, plus the first, a total of N layers. If it is not broken, will the second bead start from n + 1 layers? Obviously, no. There are-N floors left in the building, and the problem is converted to-N and two beads. Please design the most effective method.

Oh, what do you think? Ha, I think Recursion

Defines a function f (N), indicating the number of times the N floor is most effective in the worst case.

Through the above analysis

F (n) = min (max (+ f (N-1), max (+ f (N-2 )),......, Max (N-1, 1 + F (1 )))

F (1) = 1

F (100)

The solution is as follows:CodeAfter it is granted, vb2005 is used.

`  1     Dim  F (  100  )  As     Integer  , I  As     Integer , J  As     Integer    2     Dim  TC  As     Integer    3   F (  0  )  =    0    4   F (  1  )  =     1    5     6     For  I  =    2     To     100    7   F (I)  =     100    8     For  J  = I  To     1  Step  -  1    9   TC  =  IIF (J  >     1     +  F (I -  J), I,  1     +  F (I  -  J ))  10     If  TC  <  F (I)  Then  F (I)  =  TC 11     Next    12     Next    13     14     For  I  =     1    To     100    15     16   Debug. Print (f (I ))  17     Next `

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.