uva11722(概率)

题意:

两个人到达车站的时间分别是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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值