作者在 2010-03-27 11:45:31 发布以下内容
一辆卡车欲穿越过1000km的沙漠,卡车耗油为1L/km,卡车总载油能力为500L。显然卡车一次是过不了沙漠的,司机必须设法在沿途设立几个储油点,使卡车能顺利穿越沙漠。试问司机如何建立这些储油点?每一储油点应存多少油,才能使卡车以消耗最少油的代价通过沙漠?
#include<stdio.h>
#define MAX 32
void main()
{
int k,i;
float wdistance;
float storedOil[MAX];
float distance[MAX];
puts("********************************");
puts(" This programe will solve ");
puts(" the problem about storing oil");
puts("*********************************");
puts("The whole distance is 1000km,and the result is :\n");
puts("station distance(km) oil(l)");
k=1;
wdistance=500;
distance[1]=500;
storedOil[1]=500;
while(1)
{
k++;
wdistance+=500/(2*k-1);
distance[k]=wdistance;
storedOil[k]=storedOil[k-1]+500;
if(wdistance>=1000) break;
}
distance[k]=1000;
storedOil[k]=(1000-distance[k-1])*(2*k+1)+storedOil[k-1];
for(i=0;i<k;i++)
printf("%4d %6.3f %6.3f\n",i,1000-distance[k-i],storedOil[k-i]);
getch();
}
#define MAX 32
void main()
{
int k,i;
float wdistance;
float storedOil[MAX];
float distance[MAX];
puts("********************************");
puts(" This programe will solve ");
puts(" the problem about storing oil");
puts("*********************************");
puts("The whole distance is 1000km,and the result is :\n");
puts("station distance(km) oil(l)");
k=1;
wdistance=500;
distance[1]=500;
storedOil[1]=500;
while(1)
{
k++;
wdistance+=500/(2*k-1);
distance[k]=wdistance;
storedOil[k]=storedOil[k-1]+500;
if(wdistance>=1000) break;
}
distance[k]=1000;
storedOil[k]=(1000-distance[k-1])*(2*k+1)+storedOil[k-1];
for(i=0;i<k;i++)
printf("%4d %6.3f %6.3f\n",i,1000-distance[k-i],storedOil[k-i]);
getch();
}