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;
两条直线在之前的矩形中,夹在中间的面积,就是相遇的可能;
那么答案就是这部分面积除以总面积;
在用代码时间实现的时候,要分情况讨论,看看直线和矩形是怎么相交的,相交的方式不一样,求面积的方式也不一样;
两个人到达车站的时间分别是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; }
相关文章推荐
- Verilog中parameter和define的区别
- Android开发书籍推荐
- server 编程
- 单链表基本操作
- android 显示特殊符号
- 文本文件打印类库(C#)
- JS实现冒泡排序,插入排序和快速排序(从input中获取内容)
- SDK目录结构
- 学习心得
- 不可或缺 Windows Native 系列文章索引
- linux下安装rpm格式的mysql
- 小记暑假第一天
- Python中特殊函数集锦
- websphere集群在DNS主服务器宕机后异常的处理办法
- 关于java类集框架的总结
- hdu 4493 Tutor
- Visual Studio Find All no results.
- 项目的大小衡量标准,项目架构的方法(填空架子,持续集成,边开发边测试效果)(装贴)
- JS获取坐标
- android HDMI 清晰度 分辨率