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

本文介绍了一个名为CalcTime的函数,该函数用于计算多个蚂蚁在棍子上行走的最短时间和最长时间。通过输入蚂蚁的数量、位置、棍子长度及蚂蚁速度等参数,函数能够精确计算出蚂蚁从一端走到另一端所需的时间范围。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码清单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; 
          }
     } 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值