Class 1 Runtime
Each Java application hasRuntimeClass instance, so that the application can be connected to the running environment. You can usegetRuntimeMethod to obtain the current runtime.
Applications cannot create their own runtime instances.
2 train coal transportation problems
Problem Source: http://coolshell.cn/articles/4429.html/comment-page-5#comments
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?
If the weight of the coal to be transported to the market is left, we can assume that the train picks up left tons of coal somewhere, then this problem can be converted to question 2:
Condition: the train with the maximum capacity of capacity ships the coal to the pick_location location, drops left tons of coal, and returns the original location, which consumes only min_coal tons of coal.
Question 1: Calculate min_coal Based on left.
Question 2: Left is obtained based on min_coal.
Question 1
Obtain Question 1 first.
To transport left tons of coal to the market, at least the coal weight of the mining area is min_coal = distance + min_coal (capacity, left, left ).
Here, min_coal (capacity, pick_location, left) is the solution of Problem 1, indicating the train with the maximum capacity of capacity. After transporting the coal to the pick_location location, drop the left ton of coal and return to the original location, the minimum weight of coal in the mining area is min_coal when all coal is consumed.
1 left <0, min_coal = 0;
2 pick_location * 2 + Left <capacity, min_coal = pick_location * 2 + left
3 others, min_coal = pick_location * 2 + min_coal (capacity, new_left/2, new_left)
New_left = pick_location * 2 + Left-capacity. When the train goes, new_left/2 pick up new_left/2 tons of coal, and put new_left tons of coal at new_left. When the train returns, pick up new_left/2 at new_left/2.
According to this scheme, we can obtain:
When left = 529, the minimum coal volume in the mining area is 2989.
That is, the answer to this question should be at least 529.
Question 2
For question 2, assume that the mining area is a and the market is d, and there are two points between them, B and C, which can store coal.
1. Area A has 3000 tons of coal and the Maximum train capacity is 1000. That is, it needs to depart from Area A three times. Because there is only one train, it needs to return twice.
2 B has 2000 tons of coal, that is, two times from B and one time from C to B.
3 C has 1000 tons of coal, and the train goes directly to d after it is full, only once.
That is, 5ab = 1000, 3BC = 1000, AB + BC + Cd = 1000, that is, Cd = 7/15*1000, Left = 8/15*1000 = 533
The reason why the departure time is 1000 at location B and Location C is that the volume of coal in the departure fashion is 0 ~ 1000, and the linear problem is solved as a critical value. It cannot be 0 and can only be 1000.
After the results are obtained, the system verifies that 533 of the answers meet the meaning of the question, that is, at least 533 of the answers to the question.
That is, if the value of min_coal changes, the value of left also changes.
Min_coal = 2000, Left = 1000 * (1/3) = 1000*1/3
Min_coal = 3000, Left = 1000*(1/3 + 1/5 = 1000*8/15
Min_coal = 4000, Left = 1000*(1/3 + 1/5 + 1/7) = 1000*71/105
Min_coal = 5000, Left = 1000*(1/3 + 1/5 + 1/7 + 1/9) = 1000*248/315
However, when min_coal = 7000, the left> 1 calculated based on this formula, that is, more than 1000 of coal can be transported to the market. Then the critical value is reached and another formula is required.
Expansion problems:
1. The coal consumption is related to the volume of loading media.
2. Change of total media volume in the mining area.
The code for Question 1 is as follows:
# The train with the largest capacity of capacity. After transporting coal to distance, left tons of coal is left.
# Return Value: min weight of coal min_coal.
Def train (distance, capacity, left): If (left <0): Return 0; If (left <distance/3 ): return distance + left * 3 return distance + min_coal (capacity, left, left) # train with the maximum capacity of capacity. After transporting the coal to the pick_location location, drop the left ton of coal and return the original location, just consume all the coal # Return Value: min_coal, the minimum tonnage of the coal. Def min_coal (capacity, pick_location, left): Print "pick_location", pick_location print "Left:", left if (left <0 ): return 0 if (pick_location * 2 + Left <capacity): Return pick_location * 2 + Left new_left = pick_location * 2 + Left-capacity # new_left tons of coal need to be picked up on the road, new_pick_location = new_left/2 Return pick_location * 2 + min_coal (capacity, new_pick_location, new_left)