首页 > 代码库 > UVA 11722 - Joining with Friend(概率)
UVA 11722 - Joining with Friend(概率)
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; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。