A merchant needs to ride a donkey across a 1000-kilometer desert to sell 3000 carrots. It is known that a donkey can carry 1000 carrots at a time, but it will eat 1 carrot every 1 kilometer. Q: How many carrots can a merchant sell at most?

Source: Internet
Author: User

Questions:

A merchant needs to ride a donkey across a 1000-kilometer desert to sell 3000 carrots. It is known that a donkey can carry 1000 carrots at a time, but it will eat 1 carrot every 1 kilometer. Q: How many carrots can a merchant sell at most?

 

The solution of using a program here is actually very simple without a program:

 

Program description:

Both return and return are consumed. The maximum consumption is shown! When 3000 yuan is transported, it takes 5 times to travel and return, so the first 1000 yuan should be spent at 200 kilometers (the last round trip is also useful, if the food must be a radish of less than 1000), and thus the transportation is 2000, a total of three times the journey to and back, the second 1000 root is consumed at 533 kilometers (strictly at this time, there are 1001 root is consumed, 999 root is consumed, and the last root is not needed here; it depends on the limitation of Eating radish, the result may be one different). The last 1000 radishes start from 533 kilometers, so there are 533 radishes left.

 

 # Include <iostream. h> <br/> # include <math. h> </P> <p> int main () <br/> {<br/> int I = 0; // cyclic variable <br/> int S = 1000; // total mileage <br/> int n = 3000; // total number of radishes <br/> for (I = 0; I <s; I ++) <br/>{< br/> // calculate the number of shipping operations, which is the rounded down. <Br/> int x = (INT) Ceil (double) N/24/60); <br/> // if the cost of moving the radish back is higher than the number of radishes, it indicates that there is no value. Do not return and move it. <Br/> If (N % 1000! = 0 & N % 1000 <= x) <br/>{< br/> X-= 1; <br/>}< br/> // if you want to move back multiple times, the round-trip distance should be calculated. Three steps are required for two shipping operations. <Br/> If (x = 2) <br/>{< br/> X + = 1; <br/>}< br/> // if you want to move back multiple times, the round-trip distance should be calculated. It takes five times to carry three times. Of course, it would be okay to perform one operation. <Br/> else if (x = 3) <br/>{< br/> X + = 2; <br/>}< br/> // Let the donkey eat the radish. <Br/> N-= x; <br/>}< br/> cout <"the maximum number of carrots sold by sellers is:" <n <Endl; </P> <p> return 0; <br/>}< br/>

Result:

The maximum number of carrots that a merchant can sell is 534.
Press any key to continue

 

 

 

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.