题意:
有一个车站,两个人想要在这个车站见面,第一个人会在t1到t2之间的任意一个时刻到(时间上任意一点概率一样),并且停留w时间,第二个人是s2到s2的时间段到,停留也是w,问两个人的见面概率是多少?
思路:
这个应该算是个比较经典的问题了吧,感觉在那看到过,我们建立一个直角坐标系,t1<=x<=t2 ,s1<=y<=s2这样构成的这个矩形就是所有的概率区间,然后画一条x=y的直线,然后把这个直线沿着x=y方向想下和向上平移w得到一个区间,这个区间和句型重叠的部分就是见面的概率区间,用这个面积除以矩形的面积就是见面概率,求面积的时候我的方法比较笨,y=x+w,y=x-w这两条直线分别和四条线段求交点,然后在根据得到的四个交点的位置分布,枚举求出答案,我写的比较麻烦!
#include<stdio.h>
#include<string.h>
int main ()
{
double t1 ,t2 ,s1 ,s2 ,w ,x ,y;
double x1 ,x11 ,y1 ,y11;
double x2 ,x22 ,y2 ,y22;
int mark1[5] ,mark2[5];
int t ,cas = 1;
scanf("%d" ,&t);
while(t--)
{
scanf("%lf %lf %lf %lf %lf" ,&t1 ,&t2 ,&s1 ,&s2 ,&w);
int mk = 0;
memset(mark1 ,0 ,sizeof(mark1));
memset(mark2 ,0 ,sizeof(mark2));
// 1
y = t1 + w;
if(y >= s1)
{
if(y >= s2) y = s2;
if(mk <= 2)
if(mk == 0) x1 = t1 ,y1 = y;
else x11 = t1 ,y11 = y;
mark1[1] = 1;
mk ++;
}
//4
x = s1 - w;
if(x >= t1)
{
mark1[4] = 1;
if(x >= t2) x = t2;
if(mk <= 2)
if(mk == 0) x1 = x ,y1 = s1;
else x11 = x ,y11 = s1;
mk ++;
}
//3
y = t2 + w;
if(y <= s2)
{
mark1[3] = 1;
if(y < s1) y = s1;
if(mk <= 2)
if(mk == 0) x1 = t2 ,y1 = y;
else x11 = t2 ,y11 = y;
mk ++;
}
//2
x = s2 - w;
if(x <= t2)
{
mark1[2] = 1;
if(x < t1) x = t1;
if(mk <= 2)
if(mk == 0) x1 = x ,y1 = s2;
else x11 = x ,y11 = s2;
mk ++;
}
mk = 0;
// 1
y = t1 - w;
if(y >= s1)
{
mark2[1] = 1;
if(y >= s2) y = s2;
if(mk <= 2)
if(mk == 0) x2 = t1 ,y2 = y;
else x22 = t1 ,y22 = y;
mk ++;
}
//4
x = s1 + w;
if(x >= t1)
{
mark2[4] = 1;
if(x >= t2) x = t2;
if(mk <= 2)
if(mk == 0) x2 = x ,y2 = s1;
else x22 = x ,y22 = s1;
mk ++;
}
//3
y = t2 - w;
if(y <= s2)
{
mark2[3] = 1;
if(y < s1) y = s1;
if(mk <= 2)
if(mk == 0) x2 = t2 ,y2 = y;
else x22 = t2 ,y22 = y;
mk ++;
}
//2
x = s2 + w;
if(x <= t2 )
{
mark2[2] = 1;
if(x < t1) x = t1;
if(mk <= 2)
if(mk == 0) x2 = x ,y2 = s2;
else x22 = x ,y22 = s2;
mk ++;
}
if(x1 == x11 && y1 == y11 && x2 == x22 && y2 == y22 && x1 ==x2 && y1 == y2)
{
printf("Case #%d: 0.00000000" ,cas ++);
continue;
}
double m1 ,m2;
double m = (t2 - t1) * (s2 - s1);
if(mark1[1] && mark1[2]) m1 = (s2 - y1) * (x11 - t1) / 2;
else if(mark1[1] && mark1[3]) m1 = ((s2 - y1) + (s2 - y11)) * (t2 - t1) / 2;
else if(mark1[4] && mark1[2]) m1 = ((x1 - t1) + (x11 - t1)) * (s2 - s1) / 2;
else if(mark1[4] && mark1[3]) m1 = m - (y11 - s1) * (t2 - x1) / 2;
if(mark2[1] && mark2[2]) m2 = m - (s2 - y2) * (x22 - t1) / 2;
else if(mark2[1] && mark2[3]) m2 = m - ((s2 - y2) + (s2 - y22)) * (t2 - t1) / 2;
else if(mark2[4] && mark2[2]) m2 = m - ((x2 - t1) + (x22 - t1)) * (s2 - s1) / 2;
else if(mark2[4] && mark2[3]) m2 = (y22 - s1) * (t2 - x2) / 2;
double Ans = (m - (m1 + m2)) / m;
printf("Case #%d: %.8lf
" ,cas ++ ,Ans);
}
return 0;
}