Coal Transportation by train

Source: Internet
Author: User

(This article from cool shell Network: http://coolshell.cn/articles/4429.html)

The questions are as follows:

You are a coal boss in Shanxi province. You have mined 3000 tons of coal in the mining area and need to transport it to the market for sale. there are 1000 kilometers from your mining area to the market, there is a coal-burning train in your hand. This train can only hold up to 1000 tons of coal, and its energy consumption is relatively high-each kilometer consumes one ton of coal. How can you transport the most suitable coal to the market as a programmer?

 

This question seems to have no solution at first, because each kilometer of your train consumes one ton of coal, and the destination is 1000 kilometers, while the train can only load 1000 tons of media at most. If all your trains can be loaded, all the trains will be burned out at the destination. Therefore, many people think this is unlikely in their first response.

It would be normal if you thought it was not possible at the beginning. But I don't know if you will continue to think about it. If you don't want to think about it, then I am worried about you because you may not be a bad thinker, but a person who is afraid of difficulties, or who is easy to give up. This may not be suitable for a programmer who needs to think a lot.

I thought it was impossible at the beginning. I thought about it later and thought about a solution that could transport up to 500 tons of coal to the market. The method is as follows: (I hope you can think about it first before checking the answer)

 

The answer is as follows:

  1. Loaded 1000 tons of coal, took 250 kilometers, dropped 500 tons of coal, back to the mine.
  2. Load 1000 tons of coal, go to 250 kilometers, pick up 250 tons of coal and continue to 500 kilometers forward, drop 500 tons of coal, back to the mine. At this time, there are still 250 tons on the train, plus 250 tons of coal at 250 km, so the train can be returned to the mine.
  3. Mount the last 1000 tons of coal, walk to 500 kilometers, install the 500 tons of coal there, and then continue to reach the goal.

Therefore, you can transport up to 500 tons of coal to the market (of course, the train cannot go back because the mine has no coal)

It seems like this is good, but there are better ways to run more media. Do you know this method? It can be noted that the above method is used. I will not release the answer for the moment. You can think about it by yourself. In two days, I will release the answer.

 

Updated (March April 17, 2011): Everyone is very smart. 533 is the optimal solution. I used many methods to explain this process. My initial thoughts were consistent with those of my friend xpacificcoolshell! I'm glad to see a more scientific solution. In addition, there are some friends who have suggested that trains cannot be transferred anytime and anywhere, which is very good. Therefore, in the future, this question cannot be used to transport coal by train, it may be better to use the horse to run the grass .;)

 

I think like this: when a train is running, it is best to load it. The starting point is.
Step 1: transport coal to center B three times
Step 2: deliver the coal to the center point C twice
Step 3: transport coal to destination d
Step 1: 5 * (AB) = 1000; solution AB = 200
Step 2: 3 * BC = 1000; obtain BC = 333.
Step 3: AB + BC + Cd = 1000; solution Cd = 467
Therefore, we need to transport more than 533 tons of coal to our destination.

At the beginning, I also thought of putting back the problem in the middle, but it was difficult to reach the optimal value.
After thinking, my solution is like this: first, to get the maximum value, you must put down the coal equivalent to the distance at the "supply point" in the middle of the distance. So what we need to do next is to put 2000 tons of coal in the farthest place while leaving "coal supply ". Due to restrictions, the above goal can be achieved only twice. The first supply point consumes five times of coal his journey because he consumes two times, the second coal delivery consumes two times, and the last direct access to the end consumes one time. So he has to put it down at 1/5, and supplies have been provided at the first point, so the second point has to be replenished three times, and he has two and the last direct. So 200 is put down in the place of 333 + 333. So the final arrival should be 200 + 333 = 533. The total number can be derived from other values.
The programming example is:

Int sum = 3000;
Int load_num = 1000;

Int result = 0;
Int time = sum/load_num-1;
For (INT I = time; I> 0;-I ){
Result + = load_num/(I * 2) + 1 );
}

 

(Source: http://blog.csdn.net/chentaihan/article/details/6439402)

Subject content:

You are a coal boss in Shanxi province. You have mined 3000 tons of coal in the mining area and need to transport it to the market for sale. there are 1000 kilometers from your mining area to the market, there is a coal-burning train in your hand. This train can only hold up to 1000 tons of coal, and its energy consumption is relatively high-each kilometer consumes one ton of coal. How can you transport the most suitable coal to the market as a programmer?

 

This is an interview question I saw in "cool shell". It is mainly attracted by a few words from Chen Hao, and brother prefers to think about it and wants to confirm whether he is suitable for programming.

 

Analysis on coal transportation by train

On the surface, this problem is very difficult to achieve, because the train can only carry 1000 tons of coal at most, and traveling 1000 kilometers just burned out 1000 tons of coal on the train.
After careful consideration, we will find that it can be transported in segments. If 1000 tons of coal is first transported for 100 kilometers, 800 tons are put down before returning, and then 1000 tons of coal is transported to 100 kilometers,
At this time, there are still 900 tons of coal on the car, plus 100 tons of coal, there will be 1000 tons of coal on the car, there are still 900 kilometers from the end, there will be 100 tons of coal at the end.

 

Honestly, my brother has no genius IQ. I thought about this problem while reading "single men and women". It took almost an hour to get a result, I have not figured out how to obtain the 5x + 3y = 2000 (Y <= 1000/3), and calculate the maximum value of X + Y,
When I went home for dinner, I suddenly remembered this problem. I thought about it while I was eating. Sometimes I put down my bowl and painted it on my book. It took about 40 or 50 minutes,
Brother suddenly realized that 5x + 3y = 2000 should actually be 5x + 3y> = 2000. Of course, he understood the maximum value of X + Y, A few minutes later, my brother understood 5x> = 1000,
Finally, x = 200, y = 333.333333, X + Y, and the maximum value is 533.33333333.

--> 3 --> 2 --> 1
A (starting point) -------------------------------- B----------------------------------C-----------------------------D (endpoint)
<-- 2 <-- 1

| -------------------- X ----------------------- | ----------------- y ----------------- | ---------------- Z --------------- |

 

Why is it 5x + 3y> = 2000?
3000/1000 = 3. Transport 3000 tons of coal to the original site at least three times, because the more transport times, the more coal burned, and the less coal left at the end, therefore, it must be three times before the coal is transported, that is, 5x (three times, two times back ).
In the middle, you must stop at two places (B, c) to put down the coal. Because when you arrive at Location C, the maximum coal volume on the train is 1000 tons, because the train can only transport 1000 tons at most, if it fails to run more than once, it is certainly impossible to run it twice. Therefore, from A to C, at least 3000-1000 = 2000 tons of coal is burned, that is, 5x + 3y> = 2000.

 

 Why is it the maximum value of X + Y?
The remaining coal in C is only 1000 tons at most, and the more coal left near the end, the maximum value of X + Y. That is, the remaining coal = 1000-z = 1000-(1000-(x + y) = x + y

 

Why is it 5x> = 1000?

Similarly, a maximum of 2000 tons of coal is left at site B, because when the number of coal at site B is greater than 2000, it takes at least three times to transport over 2000 tons of coal from site B, three times, we think it is a --> B, so at most 2000 tons are left in B, that is, 5x> = 3000-200 = 1000.

 

Someone may ask why it is three, two, or one, instead of three or one, as shown in the above analysis, you can obtain a maximum of 400 tons for three or one times.

 

3000> = 5x + 3y> = 2000
5x> = 1000
What is the maximum value of X + Y?

 

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.