您的位置:首页 > 其它

uva11722(概率)

2015-07-27 09:28 375 查看
题意:

两个人到达车站的时间分别是t1-t2, s1-s2;并且到站后停留w分钟.问相遇的概率;

思路:

高中数学学过这个问题;首先是x轴取t1, t2,y轴取s1,s2画四条线,围出的矩形就是全集,即所有可能行;

然后两个人要相遇,则到站时间只能相差w以内;

所以画两条直线y = x + w ; y = x - w;

两条直线在之前的矩形中,夹在中间的面积,就是相遇的可能;

那么答案就是这部分面积除以总面积;

在用代码时间实现的时候,要分情况讨论,看看直线和矩形是怎么相交的,相交的方式不一样,求面积的方式也不一样;

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>

using namespace std;

double t1, t2, s1, s2, w;
double height, width;

double getArea(double W) {
double tx = s2 - W;
double bx = s1 - W;
double ly = t1 + W;
double ry = t2 + W;
bool onLeft = (ly <= s2 && ly >= s1);
bool onRight = (ry <= s2 && ry >= s1);
bool onTop = (tx <= t2 && tx >= t1);
bool onBottom = (bx <= t2 && bx >= t1);
if (onLeft && onTop) {
return (tx - t1) * (s2 - ly) * 0.5;
}
if (onLeft && onRight) {
return ((tx - t1) * (s2 - ly) - (tx - t2) * (s2 - ry)) * 0.5;
}
if (onTop && onBottom) {
return ((tx - t1) * (s2 - ly) - (s1 - ly) * (bx - t1)) * 0.5;
}
if (onBottom && onRight) {
return (height * width) - (t2 - bx) * (ry - s1) * 0.5;
}
return ly <= s1 ? height * width : 0;
}

int main() {
int t;
int cas = 1;
scanf("%d", &t);
while (t--) {
scanf("%lf%lf%lf%lf%lf", &t1, &t2, &s1, &s2, &w);
width = (t2 - t1);
height = (s2 - s1);
double ans1 = getArea(w);
double ans2 = getArea(-1 * w);
printf("Case #%d: %.8lf\n", cas++, (ans2 - ans1) / (width * height));
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: