算法 - 可以运输的最大谷物数量

时间:2012-11-19 02:32:13

标签: algorithm

我遇到了Google提出的一个我无法解决的面试问题:

  

在距离城镇N公里的绿洲上有一堆D公斤谷物。谷物需要通过骆驼车运输,其初始位置在绿洲。推车可以一次携带C千克谷物。骆驼在运输时使用谷物作为燃料。它消耗F kg / km。

     

编写一个函数,计算可运送到城镇的最大谷物量(X kg)。


我尝试使用递归,但是如果不让自己混淆,我就无法进一步了解。

这是我到目前为止所拥有的:

number of transports = N / C

fuel amount for distance D = D * F

X = N - ((number of transports) * 2 * (fuel amount for distance D))

4 个答案:

答案 0 :(得分:4)

假设N,D,C和F是输入, -

float computeMaxGrains(float N, float D, float C, float F)
{
    //Case when the cart can carry all grains at once
    if(N <= C)
    {
        float remainingGrains = N - D*F;
        if(remainingGrains >= 0)
        {
            return remainingGrains;
        }
        else
        {
            //out of fuel
            return 0;
        }
    }

    // Grains consumed per Km = (Total Number of Trips) * F
    // Total Number of Trips = 2*(N/C) + 1
    float grainsConsumedPerKm = (float) (2*(Math.floor(N/C)) + 1);

    // Remaining grains after Camels fuel = C*(N/C - 1)
    float remainingGrains = (float) (C*(Math.floor(N/C)));

    // Distance that the Camel is able to travel before the camel is 
    // 1 less round trip = 
    // (N - Remaining Grains) / grainsConsumedPerKm
    // From equation N - (grainsConsumedPerKm * distanceTravelled) = 
    // Remaining Grains
    float distanceTravelled = (N - remainingGrains) / grainsConsumedPerKm;

    if(distanceTravelled >= D)
    {
        return N - (D * grainsConsumedPerKm);
    }

    //Use recursion for every 1 less round trip
    return computeMaxGrains(remainingGrains, D-distanceTravelled, C, F);
}

答案 1 :(得分:2)

我认为这个问题最好是迭代地进行,前进。我要分出决定点。如果我正在写一个公式,我会用?:所有结​​果都是Kg。

The first question is whether there is enough grain, and cart capacity, to 
justify an initial trip from oasis to town. The camel would eat FD, so 
if FD >= min(N,C) the answer is 0

If FD < min(N,C), the net amount transferred on the initial oasis to town 
trip is min(N,C)-FD. 

The camel is now in town, the remaining grain pile is N-min(N,C), and we 
have to decide whether to send it back again. If C <= 2FD, no round trip
can be profitable.

Otherwise consider a round trip with at least C remaining at the oasis. That 
gains net C-2FD (FD put in the cart in town to keep the camel fed getting 
to the oasis, C-FD remaining in the cart when it gets back to town).

If N>C, we can do floor((N-C)/C) of those round trips, net gain 
floor((N-C)/C)*(C-2FD).

After doing the initial run and any full cart round trips, the remaining 
grain pile is N%C, the remainder on dividing N by C.

If N%C > 2FD it is worth doing a final trip to empty the grain pile, with 
an additional net gain of N%C-2FD.

答案 2 :(得分:0)

作为一个想法,虽然在绿洲中有超过D * F的谷物,骆驼将以C kg的速度行进,或者在绿洲中留下多少。骆驼在那里旅行时消耗D * F kg,放下他的负荷 - 2 * D * F kg粮食,如果剩下足够的谷物以保证回程,则返回。

答案 3 :(得分:0)

这只是猜测。我不是很确定。

设p(x)表示从源传输到距离x的最大谷物量。 然后

G(x+1)= (G(x)/C)*(C-2F) + max(F,G(x)%C - F)

现在,G(0)= N,我们需要使用上述公式找到G(D)。

第二项max(F,G(x)%C-F)表示

  1. F =当他没有回来收集剩余的谷物时 上次访问
  2. G(x)%C - F =上次访问中的剩余粮食,然后消耗F返回目的地