Questions about a group of examination algorithms (seeking optimal solutions)

Source: Internet
Author: User

1. How many planes are required (it is said that it is Microsoft's interview question)

Each plane has only one fuel tank, and the planes can refuel each other (note that there is no fuel dispenser). A box of oil can be used for an airplane to fly around the Earth for half a lap.

Q: How many planes are required to take at least one plane round the earth and return to the airport when it starts to fly?

(All planes take off from the same airport and must return to the airport safely. stopover is not allowed. There is no airport in the middle)

 

Answer:

We divide the whole course into four parts and one segment. We can know that two boxes of oil are required for a plane. Assume that aircraft a needs the whole course, the oil must be full at, so the process is considered as follows:

ABC takes off at the same time. When is reached, C returns the oil of each for AB, and returns the oil of. When AB continues to fly, B adds a 4 to 1 Oil, fills a with B, and returns the remaining 2 to 1 oil.

When a flies to 1 in 2, D goes off in the opposite direction. When 3 in 4, D adds 1 in 4 to A, and E flies in the opposite direction to 1 in 8, for each of the four parts of the AD, Ade returns the same oil.

Note: This method is the optimal solution, and other fuel filling methods cannot meet the requirements.

 

2. 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?

 

Answer:

The key to this issue is to findAlgorithmAssume that n times are required in the worst case. Then, the following formula is obtained:

When the first ball is not damaged, the first time is N layer, the second time is n + (n-1) layer, and the second time is n + (n-1) +... + (N-(n-1). Therefore, the problem is simplified:

1 + 2 + 3 +... + N, the number of floors is greater than n. The result is 14.

And so on: WHEN n = 14, the result is 105. Therefore, you can select any layer in the middle of Layer 9 to 14 for the first time. If the glass ball is not broken, the number of floors is (14-times) + the previous floor, if the glass ball is broken, the previous FLOOR + 1 starts and the next attempt is made.

For example, you can select the following sequence:, 60, 69, 90, 95, 99. In the worst case, all methods will not exceed 14 times.

Counterevidence: If 13 can be used, the sequence will change to 13, 25, 36, 46, 55, 63, 70, 76, 85, 91, which may exceed 92 when the floor is larger than 92.

 

3,There are a total of 25 horses and a game venue with 5 tracks. That is to say, a maximum of five horses can be played together. Assuming that each horse runs very stably, no otherToolsOnly through the competition between the horse and the horse, how many matches can I know the fastest five horses? (Cannot Use collision Algorithm)

A: currently, the most typical online algorithm is considered as 10 times. Later, stormspire proposed a new algorithm that is 9 times sufficient. For details, refer to http://ayygth-163-com.javaeye.com/blog/282812.

After the author's repeated calculations, 9 times is a rule-based algorithm, which is a typical optimal solution that can be achieved by using the inductive formula, but not the optimal solution that can be achieved by the current answer.

The following are the results calculated by the author. The optimal solution is eight times.

First to fifth:

Divide the 25 horses into five groups on average and match them in sequence.

Get

A1 A2 A3 A4 A5

B1 B2 B3 B4 B5

C1 C2 C3 C4 C5

D1 D2 D3 D4 D5

E1 E2 E3 E4 E5

In the sixth round, the second game in each group will be held. Assume that A2> B2> C2 is obtained, and the latter is ignored. In this way, a temporary set of up to five horses can be obtained.

{A1, B1, C1, D1, E1, A2, B2, A3, A4, A5, B3}

The second and backward CDE groups are excluded because they are slower than A1, B1, A2, B2, and C1.

The seventh time, this grouping is the key, and the horses are A3, B2, C1, D1, and E1.

There may be as many as 120 combinations of results. The following key combinations are analyzed:

Combination 1: When C1, D1, and E1 are in the top three, it is assumed that C1, D1, and E1

This result shows that the new temporary set {A1, C1, D1, E1, B1, A2} is selected for the eighth time as E1, B1, A2, and the last bit is eliminated.

 

Combination 2: When C1, D1, and E1 take the first place and the second place, it is assumed that C1 and D1

This result then generates a new temporary set based on the ranking of B2 or A3.

A3 3rd {A1, A2, C1, D1, A3, B1}: Select B1, A3, and either.

B2 3 {A1, A2, C1, D1, B1}

 

Combination 3: When C1, D1, and E1 are in the first three places, it is assumed that C1 and D1

This result then generates a new temporary set based on the ranking of B2 or A3.

A3 second {A1, A2, C1, A3, B1, D1, A4} selected B1, D1, A4 for the eighth time, excluding the last two

B2 second {A1, A2, B1, C1, B2 }.

 

Combination 4: when one of C1, D1, and E1 occupies the first place, it is assumed that C1

This result then generates a new temporary set based on the ranking of B2 or A3.

A3> B2 {A1, A2, C1, A3, B1, A4}: Select B1, A4, and either.

B2> A3 {A1, A2, C1, B1, B2 }.

 

Combination 5: When C1, D1, and E1 occupy the second and third places, it is assumed that C1 and D1

This result then generates a new temporary set based on the ranking of B2 or A3.

A3 first {A1, A2, A3, B1, C1, D1, A4, A5} selected B1, C1, D1, A4, A5 for the eighth time, excluding the last two

B2 1 {A1, A2, B1, B2, B3, C1}: Select B3, C1, and either.

 

Combination 6: when one of C1, D1, and E1 occupies the second place, it is assumed that C1

This result then generates a new temporary set based on the ranking of B2 or A3.

A3 first {A1, A2, A3, B1, C1, A4, A5} selected B1, C1, A4, and A5 for the eighth time, excluding the last two

B2 1 {A1, A2, B1, B2, B3, C1}: Select B3, C1, and either.

 

Combination 7: C1, D1, and E1 in the top three

This result then generates a new temporary set based on the ranking of B2 and A3.

A3> B2 {A1, A2, A3, B1, B2, A4, A5} selected b1, b2, A4, and A5 for the eighth time, excluding the last two

B2> A3 {A1, A2, B1, B2, B3, A3} selected B3, A3 for the eighth time.

 

The above is all the key combinations, so you can get the fastest 5 horses in 8 rounds.

PS: the question does not require the speed and order of five horses. This is the key to the answer.

 

If it becomes a general condition and the result is what, more friends are required to participate in the process.

 

 

Contact Us

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.