CSU 1602 Needle Throwing Game (投针问题)
1602: Needle Throwing Game
Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 48 Solved: 25
[Submit][Status][Web Board]
Description
There are many parallel lines on the ground with the distance of D between each adjacent two. Now, throwing a needle randomly on the ground,please calculate the possibility of that the needle can be across one of the lines.
Input
The input consists of multiple test cases. Each test case contains 2 integers D, L on a single line (1 <= D, L <= 100). The input is ended with EOF.
Output
For each test case, print an integer of (int)(P*10000) where P is the possibility asked above. For example, when P = 0.25658,you should output 2565.
Sample Input
4 2
2 4
Sample Output
3183
8372
HINT
Source
题意:给定平行线的间距和针的长度,求随机投放针,针与平行线相交的概率是多少。
分析:投针问题,公式题。
当L<D时.公式为:P=2L/Dπ
当L>=D时,P=1-(1/π)[2arcsin(D/L)-(2L/D)+(2/D)sqrt(L^2-D^2)]
#include<cstdio> #include<iostream> #include<cstring> #include<cmath> #include<stdlib.h> #include<vector> #include<queue> #include<stack> #include<algorithm> using namespace std; const double PI=acos(-1.0); int main() { double D,L,P; while(scanf("%lf %lf",&D,&L)!=EOF) { if(L<D) P=2*L/(PI*D); else P=1+(2.0/PI)*((L*1.0/D)*(1-sqrt((1-(D*D)/(L*L))))-asin(D*1.0/L)); P=P*10000; printf("%d\n",(int)P); } return 0; }