博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 4978 A simple probability problem
阅读量:5742 次
发布时间:2019-06-18

本文共 3869 字,大约阅读时间需要 12 分钟。

A simple probability problem

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 43    Accepted Submission(s): 14
Problem Description
Equally-spaced parallel lines lie on an infinite plane. The separation between adjacent lines is D (D>0). Now considering a circle of diameter D. N points lie on or in the circle. It is guaranteed that any three points are not collinear. Between any two points there's a needle. Find the possibility that, if the circle is randomly (with equal probability on any position and direction) thrown onto the same plane described above (with the equally-spaced parallel lines of separation d), at least one needle crosses a line.
 
Input
The first line contains a single integer T (1 <= T <= 100), the number of test cases.
For each set of input data, the first line gives two integers, N and D (N<=100), as described above. You can consider the center of the circle is default as the origin. Lastly N lines is followed, each containing two real numbers that representing the coordinate of a point lying within the circle.
 
Output
Each output should occupy one line. Each line should start with "Case #i: ", followed by a real number round to four decimal places, the probability that at least one needle crosses one line.
 
Sample Input
 
2 2 2 -0.5 0 0.5 0 3 3 0 1 1 0 -1 0
 
Sample Output
 
Case #1: 0.3183 Case #2: 0.5123
 
Source
题目链接  :
题目大意  :一个无限大的平面上有无数条等距平行线,每两条间距为D,给一个直径为D的圆,有n个点分布在圆上或者圆内,点的输入是依照已圆心为原点的坐标系,规定随意三点不共线。随意两点间的线段记为一根针。如今问将该圆投到平面上至少有一根针和当中一条平行线相交的概率
题目分析  :计算几何的问题,首先考虑仅仅有两点的情况即一根针。这就是一个,公式为P=2L/πD (L为针长。D为平行线间距)。再考虑多个点,显然是个凸包问题,假设凸包边上的线能够与平行线相交,凸包内的线必定能够与平行线相交,由投针问题的推广我们能够得到公式P = C/πD (C为凸包周长),详见

#include 
#include
#include
#define N 200#define inf 1e-6#define PI 3.141592653typedef struct{ double x; double y;}point;point points[N]; point chs[N]; int sp; //求凸包周长的模板double dis(point a, point b){ return sqrt((a.x - b.x) * (a.x - b.x) * 1.0 + (a.y - b.y) * (a.y - b.y));}double multi(point p0, point p1, point p2){ return (p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y);}int cmp(const void *p, const void *q){ point a = *(point *)p; point b = *(point *)q; double k = multi(points[0], a, b); if(k < -inf) return 1; else if(fabs(k) < inf && (dis(a, points[0]) - dis(b, points[0])) > inf) return 1; else return -1;}void convex_hull(int n){ int k, d; double miny = points[0].y; int index = 0; for(int i = 1; i < n; i++) { if(points[i].y < miny) { miny = points[i].y; index = i; } else if(points[i].y == miny && points[i].x < points[index].x) index = i; } point temp; temp = points[index]; points[index] = points[0]; points[0] = temp; qsort(points+1, n-1, sizeof(points[0]), cmp); chs[0] = points[n-1]; chs[1] = points[0]; sp = 1; k = 1; while(k <= n-1) { double d = multi(chs[sp], chs[sp-1], points[k]); if(d <= 0) { sp++; chs[sp] = points[k]; k++; } else sp--; }}int main(){ double sum, d; int T, n; scanf("%d",&T); for(int Ca = 1; Ca <= T; Ca++) { sum = 0; scanf("%d %lf", &n, &d); if(n == 0 || n == 1) { printf("Case #%d: 0.0000\n", Ca); continue; } for(int i = 0; i < n; i++) scanf("%lf%lf", &points[i].x, &points[i].y); if(n == 2) { double len = dis(points[0],points[1]); printf("Case #%d: %.4f\n", Ca, (2 * len) / (PI * d)); continue; } convex_hull(n); for(int i = 1; i <= sp; i++) sum += dis(chs[i-1], chs[i]); sum += dis(chs[0], chs[sp]); //算出凸包周长 printf("Case #%d: %.4f\n", Ca, sum / (PI * d)); }}

转载地址:http://kmnzx.baihongyu.com/

你可能感兴趣的文章
爱——无题
查看>>
分布式服务框架原来与实践 读书笔记一
查看>>
【http】post和get请求的区别
查看>>
TFS强制撤销某个工作区的文件签出记录
查看>>
EL表达式无法显示Model中的数据
查看>>
ps6-工具的基础使用
查看>>
灵活运用 SQL SERVER FOR XML PATH
查看>>
linux下使用过的命令总结(未整理完)
查看>>
时间助理 时之助
查看>>
英国征召前黑客组建“网络兵团”
查看>>
Silverlight 2.5D RPG游戏“.NET技术”技巧与特效处理:(十二)魔法系统
查看>>
PHP 命令行模式实战之cli+mysql 模拟队列批量发送邮件(在Linux环境下PHP 异步执行脚本发送事件通知消息实际案例)...
查看>>
pyjamas build AJAX apps in Python (like Google did for Java)
查看>>
LAMP环境搭建1-mysql5.5
查看>>
centos5.9使用RPM包搭建lamp平台
查看>>
Javascript String类的属性及方法
查看>>
[LeetCode] Merge Intervals
查看>>
Ubuntu 14.04 vsftp refusing to run with writable root inside chroot问题解决方法
查看>>
Intellij IDEA远程调试tomcat
查看>>
hadoop的学习论坛
查看>>