How monkeys move bananas

Source: Internet
Author: User

Monkeys want to move bananas home. They need to eat one banana every time they walk one rice. Monkeys can move 50 bananas once, 50 meters away from home. Now there are 100 bananas in total, how many bananas can I get at home?

 

When it has more than 50 members, move 50 members to one rice, put the rest, and move back to the other, each walk one rice to eat 3 bananas

...

When it only has less than 50 bananas, you don't have to look back. You just need to move the bananas and eat them back.

 

If the weight is n and the remaining bananas are T, when T> N, it will consume 2 * (T/N) to move the bananas 1 meter) -1, then, I will eat one, and I will take the remaining two bananas and then put them on the ground. If I am too lazy, I will take them directly)

...

When T <= N, take the bananas directly.

 

The Code has been written, and I suddenly found out, how to verify the correctness of the algorithm? Is this the method that gets the most bananas?

I think this code is very inefficient. How can I improve it!

 

 # Include <iostream> <br/> using namespace STD; </P> <p> int main () <br/>{< br/> int total, bearing, distance; <br/> cout <"input total banana:"; <br/> CIN> total; <br/> cout <"input monkey bearing :"; <br/> CIN> bearing; <br/> cout <"input distance:"; <br/> CIN> distance; </P> <p> If (bearing <= 0 | Total <= 0 | bearing <= 0) <br/>{< br/> cout <"error! "<Endl; <br/> return-1; <br/>}</P> <p> If (total <distance) <br/>{< br/> cout <"bananas are too few! "<Endl; <br/> return 1; <br/>}</P> <p> If (bearing <= 2) <br/>{< br/> If (bearing> = distance) <br/> {<br/> cout <"the monkey get home with" <min (bearing-distance, total-distance) <"Banana! "<Endl; <br/> return 0; <br/>}< br/> else <br/>{< br/> cout <"the monkey is to small! "<Endl; <br/> return 2; <br/>}</P> <p> int T = 0; <br/> int left = total; </P> <p> while (left> bearing) <br/> {<br/> If (left % Bearing> 2) <br/> left-= 2 * (left/bearing + 1)-1; <br/> else <br/> left-= 2 * (left/bearing) -1 + (left % Bearing); </P> <p> If (+ + T = distance) <br/> break; </P> <p> cout <t <"meters," <left <"bananas left! "<Endl; <br/>}</P> <p> If (T <distance) <br/> left-= distance-T; </P> <p> If (left <0) <br/> cout <"the monkey can't get home, it requires at least "<total-left <" Bananas "<Endl; <br/> else <br/> cout <"the monkey get home with" <left <"bananas! "<Endl; </P> <p> return 0; <br/>}

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.