One incorrect question in the beauty of programming: 4.1 King Kong on a plane

Source: Internet
Author: User

4.1 question about plane seat at King Kong

In other words, the solution and answer to this question are both problematic. Let's just look at the original question:

A plane is about to take off now. Passengers are preparing to follow the ticket number (1, 2, 3 ,... N) queuing for boarding in turn. Suddenly a gorilla (Yes, his name is King Kong) came ). He also had plane tickets, but he jumped into the first plane and sat down at random. According to the degree of social harmony, other passengers have two reactions:
1. All the passengers complained, "Why should I comply with Comrade Kong's rules ?" They also sat down at random and refused to give seats to other passengers.
2. although the passengers are angry, they still focus on "Harmony". If their position is not occupied, sit down quickly. If their position has been occupied by others (or Comrade Kong, just randomly select another location to sit down and start to close your eyes and raise your mind.
In these two cases, what is the probability that the I-th passenger (except Comrade King Kong) will sit at his original ticket?

 

(1) first, I think the formulas summarized by the author are too abstract. At least there are not many people who can understand the formulas.

If you encounter this problem, you can enumerate, guess the formula, and then use mathematical induction.

Suppose n = 3, that is, 1, 2, and 3.

First, analyze the probability of the first person:

If King Kong accounts for 1 with a probability of 1/3, then 1st people will never have a chance to sit in their seats. The probability is 0.

If King Kong does not account for 1 (2 or 3) -- probability 2/3, in this case, 1st people can choose between 1 and another seat (2 or 3) with a probability of 1/2, that is, P = 2/3*1/2 = 1/3.

Total: 1/3 -- this is the probability that the first person will sit in his seat.

 

Then we analyze the probability of 2nd people:

If King Kong accounts for 2 with a probability of 1/3, 2nd people will never have a chance to sit in their seats. The probability is 0.

If King Kong does not account for 2 (1 or 3), -- the probability is 2/3. In this case, we should look at 1st people in two cases:

The probability of 1st people accounting for 2 is 1/2 (because there are 2 seats left), then 2nd people will never have a chance to sit in their seats, that is, P = 0

The probability of 1st people not accounting for 2 is also 1/2, so the last seat must be 2, and the probability is 1. P = 2/3*1/2*1 = 1/3

Total: 1/3

 

Don't forget the probability of 3rd people, because there is no seat (n + 1 people, N seats, the last one is certainly no seat ).

 

In summary, I guess the answer is 1/N.

In fact, I have no bottom in my mind, so I asked N = 4 to continue the analysis, but this time, I will not list events with a probability of 0:

Probability of the first person

If King Kong does not account for 1 (accounting for 2, 3, or 4)-3/4 of the probability, in this case, 1st people can choose 1 among the remaining three seats, with a probability of 1/3, P = 3/4*1/3 = 1/4.

Total: 1/4 -- this is the probability that the first person will sit in his seat.

 

Probability of 2nd people:

If King Kong does not occupy 2 (1, 3, or 4), -- the probability is 3/4. In this case, we should look at the probability that 1st people do not occupy 2 of the remaining 3 positions: 2/3. The probability of 2nd people accounting for 2 is 1/2. P = 3/4*2/3*1/2 = 1/4

Total: 1/4

 

Probability of 3rd people:

If King Kong does not occupy 3 (1, 2, or 4), -- the probability is 3/4. In this case, we should look at the probability that 1st people do not occupy 3 of the remaining three positions: 2/3. The probability that 2nd people do not count as 3 in the remaining two positions is 1/2, and 3rd people have only one 3 left. The probability is 1. P = 3/4*2/3*1/2*1 = 1/4

Total: 1/4

 

Observe and draw a conclusion: for personal I, we first calculate King Kong, 1st people, 2nd people ...... The (I-1) individual-the probability that they do not occupy the I position, respectively (N-1)/n, (N-2)/(N-1), (N-3)/(N-2) until (n-I)/(n-I + 1), then calculate the probability that the I-th individual occupies the I-th position in the remaining n-I-th position, that is, 1/(n-I). Multiply the above Probabilities by 1/n. This is the final result.

 

Of course, when I = N, the probability is always 0, because the last one has no seats.

 

Maybe I am from the mathematics department. I think it is not the same as the computer department mm.

 

 

(2) Let's look at question 2nd:

The answer is wrong.

Continue to enumerate based on the previous steps:

In the simplest case, n = 2, that is, there are only two elements: 1 and 2.

Let's first look at the I = 1 scenario, that is, the probability of the first passenger.

If King Kong chooses 1 to sit down, the first passenger will never have a chance. It is 0.

If King Kong chooses 2 to sit down with a probability of 1/2, then only the first passenger is left, P = 1/2*1 = 1/2.

Total value: F (1) = 1/2.

 

Let's look at I = 2 again.

In other words, the probability of the last person is always 0.

 

However, according to the formula given by the author, these two values are respectively F (1) = 2/3 and F (2) = 1/2.

Therefore, I guess this formula should be f (I) = (n-I)/(n-I + 1 ). It is strange that xin did not write a few testcase to try it.

I am still confused. After all, the author is from Microsoft, so I tested the situations where I was 2 and 3 under N = 3 and n = 4 respectively.

For example, n = 4, I = 2

I found that when King Kong occupies 1st positions, the probability of 1st people not accounting for 2 is 2/3 (because there are 3 remaining). At this time, 2nd people can certainly count for 2. F (I) = 2/3

 

For example, n = 5, I = 3:

When King Kong occupies 1st locations, the following two situations are considered:

The probability of the first person accounting for 2 is 1/4, so the probability of 2nd people not accounting for 3 is 2/3, then 3rd people certainly account for 3.

The probability that 1st people do not account for 2 or 3 is 1/2. At this time, 2nd people certainly account for 2, and 3rd people certainly account for 3.

Count: 1/4*2/3 + 1/2 = 2/3

 

When King Kong occupies 2nd positions, the first person must account for 1. The probability that 2nd people do not account for 3 is 2/3. At this time, 3rd people can certainly account for 3.

Total 2/3

 

Extend N and I (here I> N:

When King Kong occupies the nth position, passengers of N-1 people are not counted, because they can certainly do their own position. Now there are n passengers and n-n passengers (from n + 1 to n ).

As a result, the problem degrades to the following: the N passengers are transformed into King Kong, and the remaining (n-n) passengers are boarding in order. The new King Kong plug-in team is the first to board the plane, sit down at random from n + 1 to n (n-n ......

The result F (n) obtained by the degraded question is the same as that of the original question F (N) (here we should make it clear that mathematical induction is required ).

So, I believe, after several degradation, because n <I <n, so n will first approach I, that is, the limit is that King Kong occupies the position of the I-1, then the possibility of personal I-1 not occupying I is (n-I)/(n-I + 1), this is the f (I) We want to calculate ).

 

To sum up, f (I) = (n-I)/(n-I + 1 ).

Then, calculate F (n) = (n-I)/(n-I + 1 ). This is the correct answer.

 

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.