Interesting algorithms: Monkey bananas

Source: Internet
Author: User
At the end of an article in the blog garden, I saw an interesting mathematical question. it is said that it was a fourth-grade elementary school question. it was cited as follows: a monkey picked 300 bananas and needed to move them home, you can move up to 100 instances at a time. The monkey is 100 metres away from home. it needs to eat one banana every 1 meter walk to supplement energy. how many bananas can it move home at most?

I saw an interesting mathematical question at the end of an article in the blog garden. it is said to be a question of grade 4 in primary school:

A monkey picked 300 bananas and needs to move them home. a maximum of 100 bananas can be moved at a time. The monkey is 100 metres away from home. it needs to eat one banana every 1 meter walk to supplement energy. how many bananas can it move home at most?

After thinking for a while, I wrote a small program to simulate this process:

$ Bananas = 300; for ($ I = 1; $ I <= 100; $ I ++) {// The first 200 ~ 300, 5 items are consumed per meter, and 20 meters can be taken if ($ bananas> 200) $ bananas-= 5; // 100 ~ When the number is 200, 3 is consumed per meter, which can take 33 meters, or (100 + 99) 53 meters of else if ($ bananas> 101) $ bananas-= 3; // There are still 101 at the end. next we can bring 100 to reach less than 47 meters (one can offset less than 0.33 meters ), so 100-47 + 1 = 54 else $ bananas --; echo 'number of remaining bananas :'. $ bananas. ', remaining distance :'. (100-$ I ).'
';} Echo $ bananas;

Program running result:

Remaining number of bananas: 295, remaining distance: 99 remaining number of bananas: 290, remaining distance: 98 remaining number of bananas: 285, remaining distance: 97 remaining number of bananas: 280, remaining distance: 96 remaining bananas: 275; remaining routes: 95 remaining bananas: 270; remaining routes: 94 remaining bananas: 265; remaining routes: 93 remaining bananas: 260; remaining routes: 92 remaining bananas: 255, remaining routes: 91 remaining bananas: 250, remaining routes: 90 remaining bananas: 245, remaining routes: 89 remaining bananas: 240, remaining routes: 88 remaining bananas: 235; remaining routes: 87 remaining bananas: 230; remaining routes: 86 remaining bananas: 225; remaining routes: 85 remaining bananas: 220; remaining routes: 84 remaining bananas: 215, remaining routes: 83 remaining bananas: 210, remaining routes: 82 remaining bananas: 205, remaining routes: 81 remaining bananas: 200, remaining routes: 80 remaining bananas: 197, remaining routes: 79 remaining bananas: 194, remaining routes: 78 remaining bananas: 191, remaining routes: 77 remaining bananas: 188, remaining routes: 76 remaining bananas: 185; remaining routes: 75 remaining bananas: 182; remaining routes: 74 remaining bananas: 179; remaining routes: 73 remaining bananas: 176; remaining routes: 72 remaining bananas: 173; remaining routes: 71 remaining bananas: 170; remaining routes: 70 remaining bananas: 167; remaining routes: 69 remaining bananas: 164; remaining routes: 68 remaining bananas: 161; remaining routes: 67 remaining bananas: 158; remaining routes: 66 remaining bananas: 155; remaining routes: 65 remaining bananas: 152; remaining routes: 64 remaining bananas: 149; remaining routes: 63 remaining bananas: 146; remaining routes: 62 remaining bananas: 143; remaining routes: 61 remaining bananas: 140; remaining routes: 60 remaining bananas: 137, remaining routes: 59 remaining bananas: 134, remaining routes: 58 remaining bananas: 131, remaining routes: 57 remaining bananas: 128, remaining routes: 56 remaining bananas: 125; remaining routes: 55 remaining bananas: 122; remaining routes: 54 remaining bananas: 119; remaining routes: 53 remaining bananas: 116; remaining routes: 52 remaining bananas: 113; remaining routes: 51 remaining bananas: 110; remaining routes: 50 remaining bananas: 107; remaining routes: 49 remaining bananas: 104; remaining routes: 48 remaining bananas: 101; remaining routes: 47 remaining bananas: 100; remaining routes: 46 remaining bananas: 99; remaining routes: 45 remaining bananas: 98; remaining routes: 44 remaining bananas: 97; remaining routes: 43 remaining bananas: 96; remaining routes: 42 remaining bananas: 95; remaining routes: 41 remaining bananas: 94; remaining routes: 40 remaining bananas: 93, remaining routes: 39 remaining bananas: 92, remaining routes: 38 remaining bananas: 91, remaining routes: 37 remaining bananas: 90, remaining routes: 36 remaining bananas: 89, remaining routes: 35 remaining bananas: 88, remaining routes: 34 remaining bananas: 87, remaining routes: 33 remaining bananas: 86, remaining routes: 32 remaining bananas: 85; remaining routes: 31 remaining bananas: 84; remaining routes: 30 remaining bananas: 83; remaining routes: 29 remaining bananas: 82; remaining routes: 28 remaining bananas: 81, remaining routes: 27 remaining bananas: 80, remaining routes: 26 remaining bananas: 79, remaining routes: 25 remaining bananas: 78, remaining routes: 24 remaining bananas: 77; remaining routes: 23 remaining bananas: 76; remaining routes: 22 remaining bananas: 75; remaining routes: 21 remaining bananas: 74; remaining routes: 20 remaining bananas: 73, remaining routes: 19 remaining bananas: 72, remaining routes: 18 remaining bananas: 71, remaining routes: 17 remaining bananas: 70, remaining routes: 16 remaining bananas: 69, remaining routes: 15 remaining bananas: 68, remaining routes: 14 remaining bananas: 67, remaining routes: 13 remaining bananas: 66, remaining routes: 12 remaining bananas: 65, remaining routes: 11 remaining bananas: 64, remaining routes: 10 remaining bananas: 63, remaining routes: 9 remaining bananas: 62, remaining routes: 8 remaining bananas: 61; remaining routes: 7 remaining bananas: 60; remaining routes: 6 remaining bananas: 59; remaining routes: 5 remaining bananas: 58; remaining routes: 4 remaining bananas: 57, remaining routes: 3 remaining bananas: 56, remaining routes: 2 remaining bananas: 55, remaining routes: 1 remaining bananas: 54, remaining routes: 0

Address of this article: http://www.nowamagic.net/librarys/veda/detail/1302,welcome.

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.