From an Internet face test (move the banana)

Source: Internet
Author: User
Tags rounds

Table of Contents1 Problem Description 2 Problem Analysis 3 program code 1 Problem Description

A, b Two cities apart from 1000Km, we have 3,000 bananas from a city to B city, known an elephant can carry up to 1000 bananas at a time, and every time the elephants go 1Km to eat a banana, if the banana was eaten, the elephant can not continue to go forward. How many bananas will you be able to transport to City B in the end? 2 Problem Analysis when I accidentally on the Web page to see such a problem, feeling a lot. Years ago when I was in an internship in an Internet company, the end was also asked a similar puzzle, but the description of the topic is not an elephant to move bananas, but monkeys move bananas. Now change the description of the object and parameters, the new. Back to this question, the crux of the problem is. First of all, if we have only 1000 bananas, is not very simple. Because the elephant every km to eat a banana, and it can move up to 1000 bananas, we can not go back, while walking and eating, finished eating. There are 0 bananas left after the destination ...

So think carefully, the crux of the problem is that the elephant can not move all the bananas at once, it has to go backwards. And go back to the time is to consume bananas. Where to return is the key point of the problem. When we had 3,000 bananas, we have to go altogether (2*3-1) = 5 times, assuming we move at this time X (3) When we have only 2000 bananas, we have to go (2*2-1) = 3 times, the distance to move is X (2), when we have only 1000 bananas, Don't go back and walk (2*1-1) = 1 times, distance is x (1). So, see the pattern.

By the analysis above:
x (n) = 1000/(2*n-1)
1000 is the number of bananas that elephants can move at most once, (2*n-1) The number of bananas consumed per kilometer (
N) for each 1000 banana, the elephant can carry all the bananas
n max is 3000/1000 = 3
1000 = x (n) + x (n-1) + ...
The last remaining banana count is:
3000-(2*n-1) *x (n)-(2*n-3) *x (n-1) ...
Since the numbers here are relatively simple, we can calculate Multi-digit:
x (3) = 1000/5 =
x (2) = 1000/3 = 333.33 More than one banana, no, go back to take the gains
x (1) = 1000-200-333 = 4
the last remaining banana number is:
3000-1000-1000-467 = 533
3 program codeWith the above analysis, it is easy to write the program. The following is the implementation of Python
#-*-Coding:utf-8-*-# N means the count of rounds # It returns the cost of rounds def cost (Per_cost, N): Return (  2*n-1) * Per_cost # Calc How long we could go with cost (n) per kilometer def f (max_load, Per_cost, n): s = max_load /Cost (Per_cost, N) return S # All_load, the number of all banana # distance, the distance between the cities # max _load, the max loading elephant could carry # per_cost, the cost of Banana KM def calc_remain (all_load, distance, max_
        Load, per_cost): remain_dist = Distance Nmax = All_load/max_load All_cost = 0 while Nmax > 0: If Remain_dist > F (max_load, Per_cost, nmax): Remain_dist = f (max_load, Per_cost, Nmax) All_
            Cost = max_load Nmax-= 1 Else:all_cost + = Cost (Per_cost, Nmax) * remain_dist Remain_dist = 0 Nmax = 0 print ' Remain load:%d, remain distance%d '% ((all_load-all_co ST), remain_dist) reTurn (all_load-all_cost), remain_dist if __name__ = = ' __main__ ': all_load = 3000 distance = 1000 Max_load
 = 1000 Per_cost = 1 rload, rdist = Calc_remain (all_load, distance, max_load, per_cost)
4 Thought expansion

With the above procedure, we can quickly solve other variants of this problem. For example, an elephant can move at least one banana at a time to ensure that the banana is left after the destination. It would be a bit harder if the problem were to be this way. Similarly, invoking the code we have written above can solve the problem quickly.

if __name__ = = ' __main__ ':
    all_load = 3000 distance = 1000 max_load
    = 1000 Per_cost
    = 1 for
    
    i in range (1000):
        rload, rdist = Calc_remain (all_load, distance, I, 1)
        If rdist = 0:
            print "i =%d."% i
            break

HTML generated by Org-mode 6.31a in Emacs 23

Related Article

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.