從A到B的過程要坐船經過幾條河 ,每條船的初始位置和行駛方向是隨機等概率的 。求從A到B的期望時間?
分析:
假設:河寬L, 船速V , 初始位置X ,過河時間t .
則 E[ t ] =
可見平均過河時間只與河寬和船速有關。
參考代碼:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int main()
{
int N , D ,cas = 1;
while(scanf("%d%d" ,&N , &D) ,N+D){
double t = 0 , s = 0 , p , l ,v;
for(int i=0;i<N;i++){
scanf("%lf%lf%lf" ,&p , &l ,&v);
t += 2*l / v;
s += l;
}
t += D - s;
printf("Case %d: %.3f\n" ,cas++ , t);
printf("\n");
}
return 0;
}