C语言高手进 2.1 已知某长途车从始发站早6:00到晚6:00每1小时整点发车一次.正常情况下,汽车在发车40分钟后停
首页/题库/171℃/2024-05-11 12:24:26
C语言高手进
2.1
已知某长途车从始发站早6:00到晚6:00每1小时整点发车一次.正常情况下,汽车在发车40分钟后停靠本站.由于路上可能出现堵车,我们假定汽车因此而随机耽搁0-30分钟.也就是说假定最坏情况汽车在发车70分钟后才到达本站.
假设某位旅客在每天的10:00到10:30之间一个随机的时刻来到本站,那么他平均的等车时间是多少分钟?
您可以通过编程多次模拟这个过程,计算输出平均等待的分钟数.精确到小数后1位.
优质解答:
#include
#include
#include
// 精确地分钟的时间类
// 采用24小时制
struct Time
{
\x05int hour;
\x05int min;
};
//长途车早6:00开始发车
const Time BusStartTime = {6,0};
//长途车晚6:00结束发车
const Time BusEndTime = {18,0};
// 发车间隔为1小时
const Time BusIntervalTime = {1,0};
// 长途车正常到站时间 40分钟
const Time BusNormalArriveTime = {0,40};
// 长途车晚点到站的最小最大时间范围
// 最少延迟 0 分钟
const Time BusDelayArriveMinTime = {0,0};
// 最大延迟 40 分钟
const Time BusDelayArriveMaxTime = {0,40};
// 旅客到站等车的时间区间
// 旅客到站等车的最早时间
const Time PassengerWaitEarliestTime = {10,0};
// 旅客到站等车的最晚时间
const Time PassengerWaitLastestTime = {10,30};
// 模拟旅客等车次数
const int SimulateTimes = 5;
/*
比较两个时间 t1 和 t2 的早晚
t1 比 t2 早,返回负数
t1 比 t2 晚,返回正数
t1 和 t2 同时,返回0
*/
int compareTime(struct Time t1,struct Time t2)
{
\x05return (t1.hour * 60 + t1.min) - (t2.hour * 60 + t2.min);
}
/*
\x05在 t1 的基础上加上 t2
\x05结果不会超出24小时制
*/
Time addTime(struct Time t1,struct Time t2)
{
\x05Time result ;
\x05result.min = (t1.min + t2.min)%60;
\x05result.hour = ((t1.hour + t2.hour) + (t1.min + t2.min)/60)%24;
\x05return result;
}
/*
\x05在 t1 的基础上加上 t2
\x05结果转换成分钟 (小时为0)
*/
Time addTime2(struct Time t1,struct Time t2)
{
\x05Time result ;
\x05 result.hour = 0;
\x05 result.min = (t1.hour*60 + t1.min) + (t2.hour*60 + t2.min);
\x05return result;
}
/*
\x05在 t1 的基础上减去 t2
\x05结果转换成分钟(小时为0)
*/
Time subTime2 (struct Time t1,struct Time t2)
{
\x05 Time result ;
\x05 result.hour = 0;
\x05 result.min = (t1.hour*60 + t1.min) - (t2.hour*60 + t2.min);
\x05 return result;
}
/*
\x05产生介于 tMin 和 tMsx 之间的一个随机时间
*/
Time randTime(struct Time tMin,struct Time tMax)
{
\x05Time result ;
\x05
\x05int tMinAsMinute = tMin.hour*60 + tMin.min;
\x05int tMaxAsMinute = tMax.hour*60 + tMax.min;
\x05int randTAsMinute = rand()%(tMaxAsMinute - tMinAsMinute ) + tMinAsMinute;
\x05result.hour = randTAsMinute/60;
\x05result.min\x05= randTAsMinute%60;
\x05return result;
}
/*
\x05时间 t 和 n 取平均
*/
int averageTime(struct Time t,int n)
{\x05
\x05return (t.hour*60 +t.min) / n;
}
/*
输出时间 t
*/
void printTime(struct Time t)
{
\x05printf(" [%2d :%2d] ",t.hour,t.min);
}
int main(int argc,char *argv[])
{
\x05Time busStartTime;
\x05
\x05Time busNormalArriveTime;
\x05Time randDelayTime;
\x05Time busRealArriveTime;
\x05
\x05Time passengerWaitStartTime;
\x05Time passengerMinWaitTime;
\x05Time passengerGetOnBus ;
\x05Time passengerTotalWaitTime = {0,0};
\x05
\x05int t ;
\x05// 模拟次数
\x05for(t = 0 ; t < SimulateTimes ; t ++)
\x05{\x05
\x05\x05srand(time(NULL)+t);
\x05\x05// 输出模拟第几天
\x05\x05printf("SimulateDay = %6d\n",t + 1);
\x05\x05// 乘客当天到站等车的开始时间
\x05\x05passengerWaitStartTime = randTime(PassengerWaitEarliestTime,PassengerWaitLastestTime);
\x05\x05// 输出乘客当天到站等车的开始时间
\x05\x05printf("passengerWaitStartTime = ");
\x05\x05printTime(passengerWaitStartTime);
\x05\x05printf("\n");
\x05\x05
\x05\x05// 乘客当天能等车花费的时间,初始化为一个最大值(24小时)
\x05\x05passengerMinWaitTime.hour = 24;
\x05\x05passengerMinWaitTime.min = 59;
\x05\x05
\x05\x05// 模拟一天的发车过程
\x05\x05for(\x05busStartTime = BusStartTime ;
\x05\x05\x05\x05compareTime(busStartTime,BusEndTime)= 0 &&
\x05\x05\x05\x05 \x05\x05compareTime(subTime2(busRealArriveTime,passengerWaitStartTime),passengerMinWaitTime)
我来回答修改/报错/举报内容!
猜你喜欢
- 蜂群有蜂王、工蜂、雄蜂,这是蜜蜂的什么行为?
- 把一块长与宽的比为7:2的长方形晒谷场,用1/500的比例尺画在图上,已知这张图上长方形的周长是90cm
- 一个感人的故事作文 我谢你一辈子!
- ——how is it going?joyce——( ) A,oh okay i guess B ,not doing
- 已知a的平方+a+1=0,试求a的1000次方+a的2001次方+a的3002次方的值
- 秋思改成故事300字
- 我想大家帮我设计一个英文名 希望你们的设计和我的中文名类似!
- 若方程x-5/x-6-x-6/x-5=K/x2-11x+30的解不大于15,求K的取值范围.
- 英语翻译Yesterday,the class bell of our school changed into a pi
- 关于把字句,请问,我洗衣服了,和我把衣服洗了.有什么差异.
- 文学,你是我的最爱...
- 我很高兴,因为我认识了新的老师和同学 这句话的英语翻译