Small Lotus only dew pointed point, early dragonfly standing above
--Koike
The problem is described in this way:
Shanxi coal boss has 3000 tons of coal, to be transported to 1000km kilometers away from the place to sell. He chose to use a train to carry coal, and one kilometer per train would consume a ton of coal, with a maximum of 1000 tonnes of cargo on the train.
Shanxi Coal boss is a code-savvy guy, how much coal do you think it can pull over?
And no matter who knows the code why should choose such a painful way to pull coal. Forget it, just abstract it into a mathematical problem.
Shanxi coal boss's total coal is amount, the total mileage is totaltrail, the train load is load
There are hidden conditions for this type of problem:
1, assuming that each car can be filled with goods, then the amount must be an integral multiple of load. (as this can maximize delivery of goods)
2, the vehicle must be every step to consume an item. (Because the coefficient is 1 the simplest, but in fact the coefficient is flexible, and it is not troublesome.) )
The example of this problem is relatively simple, mental arithmetic is also stress-free, mainly talk about ideas.
3000T coal first with 3 cars pulled full, line to 1000/3 (km), three vehicles total loss of load 1000t Coal, parking, the remaining 2000t of coal loaded to 2 cars, then forward 1000/2 (km), and lost 1000t coal. The last 1 cars pull this 1000t of coal away.
∴amount = 1000-1000 (1-1/2-1/3) =833.3t coal
The code is implemented with recursion to solve two exits:
1. Recursion ... Last bus, pull over, finish.
2. Recursion ... Last few cars, pull over, finish
1 Package Cn.train;2 3 Public classTrain {4 Public Static voidMain (string[] args) {5 intHuowu = the;6 intLoad = +;7 DoubleTotaltrail = +;8Huowu = Calculate (Huowu, load,0.0, totaltrail);9System. out. println ("the remaining goods are"+Huowu);Ten } One Public Static intCalculate (intHuowu,intLoadDoubleTrailDoubletotaltrail) { A intQuantity = Huowu/load;//calculate the number of cars - //the last car will be finished . - if(Totaltrail-trail < load/quantity) { theHuowu-= quantity* (totaltrail-trail); - returnHuowu; - } - //one last car. + if(Huowu = =load) { -Huowu-= (int) (Totaltrail-trail); + returnHuowu; A } atTrail + = load/quantity;//It's starting to go. -Huowu-=load; - returnCalculate (Huowu, load, Trail, totaltrail); - } -}
Java Learning Notes--the problem of the cart of Shanxi coal boss's egg