码迷,mamicode.com
首页 > 其他好文 > 详细

UVA 11722 - Joining with Friend(概率)

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

标签:style   http   color   os   io   代码   

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(概率)

标签:style   http   color   os   io   代码   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!