[Beauty of programming] diamond plane ride

Source: Internet
Author: User

Description

Now there is a plane to take off, passengers are preparing to queue for boarding at a time according to the air ticket number (, 3..., n. Suddenly a gorilla (King Kong) came ). He also had a plane ticket, but he jumped into the first flight and sat down at random. According to the degree of social harmony, other passengers have two reactions:

1. All the passengers were angry and said, "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 then start to close your eyes and raise your mind.

Q: In these two cases, what is the probability that the first passenger (out of Comrade Kong) will be able to position his original ticket?

Answer

Question 1: This problem is equivalent to a sorting problem. The total number of sorting tasks is N !, If the I do the I position, n-1 individual full row column (N-1 )!, To sum up the probability of seeking for (N-1 )! /N! = 1/N.

Question 2: "The beauty of programming" is complicated and I don't know much about it. I found a few blog posts on the Internet to answer this question. It is easier to understand it in this way:

Hypothesis:F (I, n) indicates the probability that an I-th individual will have exactly the I-th seat if there are n seats;

P (k = J) indicates the probability that King Kong is sitting on Position J;

P (I | K = J) indicates that under the premise that King Kong achieves position J, the I-th person happens to have the probability of the I-th position.

The above assumptions are based on the full probability formula:

Because the probability of King Kong sitting at each position is equal, it is easy to know that P (k = J) = 1/N;

Next, we only need to consider the value of the probability of the next condition.

(1) If j = 1, it indicates that King Kong is sitting in the first position, then the probability of I sitting in the I position is 1. If j> I, the people in front of it must sit at the position, so the probability is also 1. therefore, we only need to consider the case of 1 <j <I, see.

(2) In the case of 1 <j <I, that is, King Kong is sitting at the position J (1 <j <I, then, Unless J passengers are sitting in the position of King Kong, they also need to seize the seats of others. This is similar to King Kong's behavior (because King Kong will seize others' seats unless it is in its own position), so we can talk about the J-th passenger as the new King Kong, at this time, there are still n-J seats left, and the number of the remaining passengers is also minus J, then the previous I-th passenger seat number is changed to I-j, which is similar to the original problem, the condition probability is f (I-j, N-j ).

Therefore, there are the following equations:

Then, the recursive formula F (I, n) = f (I-1, n-1) can be derived from the above formula ). (I have verified that the formula is correct, but it will not be deduced. I hope some netizens will give you some advice ).

In the above recursive formula, we can: NF (I, n) = (n-I + 1) + (I-2) f (I, n ), the answer to the question is:

References:

1. The beauty of programming P265-P267

2. http://hi.baidu.com/maxint/blog/item/8ac3c49707911f6354fb96e9.html

3. http://blog.sina.com.cn/s/blog_7cd1ba470100rgkz.html

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.