### An interesting question

Hi,everybody:
several days ago,I meet a problem. Here is the description:
A truck want to cross a desert , the length of this desert is 1000 km. The truck can only take with 500L oil once. The truck will consume 1L oil per kilometer .Now, obviously, the truck can not pass through this desert, so it will be necessary to set some oil station while the truck goes ahead. The question is how to set these station so that it can use the least oil .
What? This has nothing to do with C++...
It was asked to be solved with C++ language...
You could stick one station at 500km, or one 250km and 750km. Or one every 100km. Doesn't really matter since the truck spends a set amount of oil per km. This problem is silly and doesn't need any kind of programming
The key point here is that the station starts with no oil; the truck has to deliver oil to the station itself. There is effectively a station at zero km with an infinite amount of oil that the truck can return to to refuel.
Last edited on
@ResidentBiscuit
:You didn't understand this question's meaning please see Moschops's comment.
Oooh, that makes more sense. I didn't see any mention of that piece of information in the problem
closed account (zb0S216C)
Driving at a low speed in a low gear consumes less fuel.

Wazzak
I have an idea~~~~
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071`` ``````#include typedef struct GasStation { float GasCapacity; float DisFromLast; } GasStation; /** * 算法:以尽可能少的趟数完成任务 * 初始条件:最后一个station肯定是在中点,即距离起始点500km * 最后一个station储油的计算: * 假设前一个储油点距离现在这个点x km,那么每走一趟(一来一回)能送500-2x * 需求是能有500L以使车子能够开到终点,那么按照需求最少,假设只运一趟,即走3x, * 那么3x+500为上一个station的理论储油量 * 又因为500-2x+500-x=500; ==> x=500/3,它小于250,说明是可行的,即只运输一次能够完成要求 * 最后一个station的实际储油量是 * 500-2x=500-1000/3=500/3 * 以这种分析方式直到符合所有要求 * */ void SetGasStation(GasStation *station,float *RequestOil); int main() { GasStation station[32] ; // 初始化 float RemainDistance = 500 ; float RequestOil = 500 ; int i = 0 ; while(RemainDistance>0) { SetGasStation(&station[i],&RequestOil); RemainDistance-=station[i].DisFromLast; printf("%.3f \t\t",station[i].GasCapacity); printf("%.3f\n",station[i].DisFromLast); i++; } } void SetGasStation(GasStation *station,float *RequestOil) { // 从初始的第一次开始进行尝试, 即初始化最小趟数认为是只放一次 int Times = 1 ; float OnceDistance; // 假设到前一个station的距离为X,即station.DisFromLast=x; // 由于尝试的次数为t,那么储油量为(500-2x)*t+500-x=RequestOil // 求解x的值，如果x大于250,则为无效数据,将Times递增,继续循环做下去 OnceDistance = (500*Times+500-(*RequestOil))/(2*Times+1); while(OnceDistance>=250 || OnceDistance<=0) { // 不合格数据 Times++; OnceDistance = (500*Times+500-(*RequestOil))/(2*Times+1); } // 直到数据合格之后 (*station).DisFromLast = OnceDistance; (*station).GasCapacity = (500 - 2*OnceDistance)*Times; (*RequestOil) = 500 * Times + 500; }``````

Since I'm Chinese,so I use chinese character to write this comment~~
Topic archived. No new replies allowed.