首页 > 其他 > 详细

UVA 11722 - Joining with Friend(概率)

时间:2014-07-17 15:31:52      阅读:373      评论:0      收藏:0      [点我收藏+]

UVA 11722 - Joining with Friend

题目链接

题意:你会在[t1,t2]时刻到,你朋友会在[s1,s2]时刻到,两个人都停留w,问两人碰面的概率

思路:概率题,画图,计算围成面积/总面积就是概率

代码:

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

int t;
double t1, t2, s1, s2, w;

double cal(double w) {
	double ly = t1 + w;
	double ry = t2 + w;
	double ux = s2 - w;
	double dx = s1 - w;
	if (ly >= s2) return 0;
	if (ry <= s1) return (t2 - t1) * (s2 - s1); 
	bool isleft = (ly >= s1 && ly <= s2);
	bool isright = (ry >= s1 && ry <= s2);
	bool isup = (ux >= t1 && ux <= t2);
	bool isdown = (dx >= t1 && dx <= t2);
	if (isleft && isup) return (ux - t1) * (s2 - ly) * 0.5;
	if (isleft && isright) return (s2 - ly + s2 - ry) * (t2 - t1) * 0.5;
	if (isdown && isright) return (t2 - t1) * (s2 - s1) - (t2 - dx) * (ry - s1) * 0.5;
	if (isdown && isup) return (ux - t1 + dx - t1) * (s2 - s1) * 0.5;
}

int main() {
	int cas = 0;
	scanf("%d", &t);
	while (t--) {
		scanf("%lf%lf%lf%lf%lf", &t1, &t2, &s1, &s2, &w);
		printf("Case #%d: %.7lf\n", ++cas, (cal(-w) - cal(w)) / (t2 - t1) /(s2 - s1));
	}
	return 0;
}


UVA 11722 - Joining with Friend(概率),布布扣,bubuko.com

UVA 11722 - Joining with Friend(概率)

原文:http://blog.csdn.net/accelerator_/article/details/37903059

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!