Car fuel-a confusing interview question

Source: Internet
Author: User

Problem

A 500-litre car is driven from A to B, 1000 kilometers away. It is known that the fuel consumption of a car is 1 litre per kilometer, and there is an infinite amount of oil at, there is no oil in any other place, but the car can store oil in any place for transshipment. How much oil is needed from A to B?

Analysis

At the beginning of this question, I thought the solution was too flexible and a little dizzy. All the answers to this question on the Internet are the same:

The question can be attributed to the number of series an = 500/(2n + 1) n = 1000, 2, 3... and when is the sum of Sn and n greater than or equal?

When n = 6, S6 = 977.57

Therefore, the distance between the first vertex and the starting position is 1000-977.57 = 22.43 kilometers.

So before the first transit, a total of 22.43*(2*7 + 1) = 336.50 liters of oil consumed

After that, each transit consumes 500 liters of oil

Therefore, the total fuel consumption is 500 + 336.50 = 3836.50 liters.

It seems reasonable, but it does not explain why? Why is that? How to prove it?

But let's simply put it down: 3836.50-7 * (500-2*22.43) = 650.52, that is to say, 650.52 will be left for the first transit 7 times, proving that this answer is wrong!

Then I guess I want to come up with an explanation. First, we need to grasp several key points:

1. the transportation distance should be as short as possible, that is to say, our solution is to first move all the required oil from the starting point to the first transshipment point X km away, and then, and so on, you cannot rush back and forth to multiple transshipment sites.

2. The transportation efficiency should be as high as possible, that is to say, to run with the maximum transport capacity-do not run again because of 1 or 2 liters of oil.

3. The number of adjacent transshipment points must be an odd number.

As a result, we can consider the following:

1. When the vehicle reaches the end point 1000 kilometers away, you only need to put 0 of the oil there. The transfer count is 1 and the distance is 500;

2. When a vehicle arrives at a transshipment point 500 kilometers away, it needs to put 500 of the oil there, and the number of transshipment times is 3. How can we ensure that the three transportation times are the most efficient? The distance between transshipment points can be obtained by ensuring the highest transportation efficiency. This is also the case above.

3. and so on... Number of transfers 1 3 5 7...

Solution 1

/// <Summary>
/// Calculate the oil a placed in the distance from X, and the maximum transportation efficiency is 2n + 1.
/// </Summary>
Double distance (double A, int N)
{
Return (tanklimit * (n + 1)-A)/(2 * n + 1 );
} // X is the distance, A = K (tanklimit-2x) + 500-x

/// <Summary>
/// The total number of times the amount oil needs to run in the distance from X
/// </Summary>
Int trip (Double X, double amount)
{
Int n = 0;
While (true)
{
VaR a1 = 2 * n * (tanklimit-2 * X); // The amount of fuel to and from
VaR a2 = tanklimit-X; // The amount of fuel for the last operation
VaR diff = A1 + A2-amount;
If (diff> = 0 |-diff <= 0.0001) return 2 * n + 1; // avoid calculation error
N ++;
}
}

Void transport () // Reverse Transfer
{
Int K = 0;
Double A = 0, D = 0; // A indicates the total fuel consumption of each transit, and D indicates the total distance.

While (d <1, 1000)
{
VaR last_a =;
VaR x = distance (A, 2 * k );
If (D + x <= 1000)
{
A + = (2 * k + 1) * X;
K ++;
}
Else
{
X = 1000-D;
A + = trip (X, A) * X;
}
D + = X;

Console. writeline ("A = {0: f} X = {1: f} trip = {2}", A, X, trip (x, last_a ));
}
}

Run the program and you can obtain:

A = 500.00 x = 500.00 trip = 1

A = 1100.00 x = 200.00 trip = 3

A = 1877.78 x = 155.56 trip = 5

A = 2751.28 x = 124.79 trip = 7

A = 2888.89 x = 19.66 trip = 7

It can be seen that if this transportation method is used, it is much less than the answer posted on the Internet: 3836.50 liters. However, such 1 3 5 7... Is the transportation method optimal?

Solution 2

Maybe we don't need 1 3 5 7... As this increments so fast, 1 3 3 3 5 5 5 may also be a good method, so let's make minor changes to the program:

Double transport (INT factor)
{
...
VaR lasta =;
VaR x = distance (A, 2 * k );
If (x = 0) {k ++; continue ;}
If (D + x <= 1000)
{
A + = (2 * k + 1) * X;
If (CNT ++ % factor = 0) K ++;
}
...

Then calculate the factor from 1 ~ 100 of the fuel consumption is:

1 2888.88888888889

2 2507.65432098765

3 2376

4 2350.4

5 2340.16

6 2336.064

7 2334.4256

8 2333.77024

9 2333.508096

10 2333.4032384

11 2333.36129536

12 2333.344518144

13 2333.3378072576

14 2333.33512290304

15 2333.33404916122

It can be seen that factor> 15 will become stable.

Discussion

From this we can see that we cannot blindly trust others' solutions (including my current solutions, of course). Here we can guess that solution 2 cannot be the best solution, there may be some better sequences-Haha. Here, we should think that this problem is best suited to Genetic Algorithms! I have no time for the moment. If you are interested in finding a better solution, please let me know :)

 

Author: Silver source link: http://yishan.cc/blogs/gpww/archive/2009/10/28/1-1.aspx

-------------------------------------

The beauty of programming-Microsoft technical interview experience source code:
Http://download.csdn.net/source/1883633 http://www.broadview.com.cn/06074


In addition, please give me a favor and vote for it. Let's take a look at the question most liked by everyone in Chapter 1st "the joy of the game-the question encountered in the game, or the question that everyone thinks is the most interesting.

I would like to thank everyone for choosing ^_^ (you can select any of the following websites to vote)

Csdn: http://vote.csdn.net/VotePost.aspx? Voteid = 6419

Kaixinnet: http://www.kaixin001.com /~ Vote/detail. php? Vid = 6324994 & uid = 57936198

QQ space: http://user.qzone.qq.com/80754098/vote/1021116304

Other articles:
Serialization 1: Catalan)
Serialization 2: String set corresponding to sequence Abab
Serialization 3: Longest Common subsequence
Serialization 4: Calculate string Similarity
Serialization 5: search for qualified Integers
Serialization 6: array Cyclic Displacement
Serialization 7: Tianping Scale Ball
Serialization 8: a dynamic and ordered collection -- challenge the red/black tree
Serialization 9: Find the maximum number of K
Serialization 10: Sorting of a pile of cakes
Serialization 11: Overview of the discrete optimization problem search framework
Serialization 12: the timing, minute, and second needles overlap-seemingly simple interview questions

 

 

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.