C++编程之美-数学之趣(代码清单4-3)

代码清单4-3

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 = 0; Min = TotalTime;
     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; 
          }
     } 
}
发布了1247 篇原创文章 · 获赞 951 · 访问量 7万+

猜你喜欢

转载自blog.csdn.net/weixin_42528266/article/details/104028239