Detailed answers to innovative factory interview questions: how many fish are involved-positive solution recursive Solution

Source: Internet
Author: User

Today, I read a blog on innovative workshop interview questionsQuestion: ABCDE five-member fishing, sleep, a woke up, threw away one fish, divided the rest into five points, took one, and left; B woke up and threw away one, divide the remaining parts into five portions and take one portion. Then CDE obtains the fish according to the above method. How many fish have they caught?

It is interesting that many of the students participate in the solution. I have read the answers from the participants and found that most of them use the looping method. Although the solution can be obtained, it is not the correct solution, this question has a simple mathematical solution: x = 5 ^ 5-(5-1 );

This topic can be promoted to m people. Suppose the first person has X1 fish before the fish, and the second person has X2 fish before the fish ..... m people have XM fish before the fish, if the number of fish to increase the M-1, each person can be equal to take their own copy (no need to throw one ), in this way, you can export:


X1 = m ^ m * XM + 1/(M-1) ^ (M-1 );

And XM + 1/(M-1) ^ (M-1) of the smallest integer is 1, so X1 = m ^ m, and then subtract the added M-1, got the answer.

However, the programming question is whether you will use recursive algorithms.

The following formula can be obtained based on the question:

X1-(X1-1)/m-1 = x2 export X1 =
(M * X2 + M-1)/(M-1), further can be pushed out xn = (M * Xn-1 + M-1)/(M-1 ).

According to the meaning of the question can also know that the last person after the separation, the number of fish can be M-1 personal evenly, that is, XM + 1 = (M-1) * k, K =, 3 .... an integer greater than 0. In this way, the XM + 1 value is known for the determined m and K, so that the problem can be solved by recursion.

The efficiency of solving such problems is high. For M = 5, the solution X1 = 255 can be obtained only after 3121 cycles.

In order to increase interest, after the program is resolved ~ XM is output.

For details, refer to the following code. This program uses recursive algorithms, which is not easy to understand. Please read them carefully. recursive Programs are the basic skills of programmers.

But this is specific gadgets, games only, to become an excellent software workers, or to learn more coding is the design of the theory: see http://blog.csdn.net/xabcdjon/article/details/6707050 and http://blog.csdn.net/xabcdjon/article/details/6823653

Public class disfish {static int M = 5; // Number of people static int n = 2; // expected results static int result [] = new int [m]; public static void main (string [] ARGs) throws exception {int n = 0; int K = 1; while (n <n) {int X1 = calculatex (2, K ); // X1 = f (X2) X1 is a function of X2. If (x1> 0) {n ++; Result [0] = x1; outres (k) ;}k ++ ;}} /*** recursive Xm-1 = (M * XM + M-1)/(M-1) * @ Param M * @ Param k Positive Integer * @ return-1 indicates that the integer k has failed to be tested, because the remaining quantity cannot be evenly shared by the M-1 individual */static int calculatex (int m, int K) {int XM =-1; if (M> m) {int x = (M-1) * k; XM = m * x/(M-1) + 1; return XM;} int xplus = calculatex (m + 1, k ); // recursively solve XM + 1if (xplus <0) Return-1; int r = m * xplus % (M-1); If (R! = 0) Return-1; // the remaining quantity cannot be evenly allocated by the M-1 individual XM = m * xplus/(M-1) + 1; Result [M-1] = XM; // Record XM return XM;} static void outres (int K) {system. out. println ("k =" + k); For (INT I = 0; I <result. length; I ++) {int P = I + 1; system. out. print ("X" + P + "=" + result [I] + "");} system. out. println ();}}

You can also try to use the recursive formula after adding a M-1 to solve the problem, to deepen your understanding of the recursive algorithm.

 

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.