蚂蚁爬杆

来源:互联网 发布:凡高网络 编辑:程序博客网 时间:2024/06/11 19:37

问题:

解答:




伪代码如下:

void CalcTime(double Length,// length of the stick
double *XPos,//position of an ant,<=length
int AntNum,  //number of ants
double Speed, //speed of ants
double &Min,//return value of the minimum time
double &Max)//return value of the maximum time
{
//parameter checking ,Omitted
//total time needed for traveling the whole stick
double TotalTime = Length / Speed;
Max = Min = 0;
for (int i = 0; i < AntNum; i++)
{
double currentMax = 0;
double currentMin = 0;
if (XPos[i]>(Length / 2))
currentMax = XPos[i] / Speed;
else
currentMax = (Length - XPos[i]) / Speed;
currentMin = TotalTime - Max;
if (Max < currentMax)
Max = currentMax;
if (Min < currentMin)
Min = currentMin;
}
  
}


0 0
原创粉丝点击