牛吃草 数论

来源:互联网 发布:单片机处理at指令 编辑:程序博客网 时间:2024/04/30 14:50

4243: 牛吃草

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 306  Solved: 87

Description

农夫有一个长满草的(x0, y0)为圆心,r为半径的圆形牛栏,他要将一头牛栓在坐标(x1, y1)栏桩上,但只让牛吃到一半草,问栓牛鼻的绳子应为多长?

Input

输入一个T,表示T组测试数据
下面T行每行五个整数 x0, y0, x1, y1, r 所有数据的绝对值小于1e5

Output

每组测试数据输出绳子长度,保留4位小数

Sample Input

20 0 0 0 20 0 10 10 2

Sample Output

1.4142

14.1892

#include<cmath>#include<iostream>#include<cstring>#include<cstdio>using namespace std;const double eps=1e-8;const double pi=acos(-1.0);int dsgn(double x){return x<-eps?-1:x>eps;}double Area(double r,double R,double l){if(dsgn(l-r-R)>=0) return 0;else if(dsgn(l-fabs(r-R))<=0){if(r>R) r=R;return pi*r*r;}double a=acos((l*l+r*r-R*R)/(2*l*r));double b=acos((l*l+R*R-r*r)/(2*l*R));double s1=a*r*r,s2=b*R*R;double S1=r*r*sin(a)*cos(a),S2=R*R*sin(b)*cos(b);return s1+s2-S1-S2;}int main(){int t;cin>>t;while(t--){   double x0,x1,y0,y1,r;   scanf("%lf%lf%lf%lf%lf",&x0,&y0,&x1,&y1,&r);   x1-=x0,y1-=y0;   double l=sqrt(x1*x1+y1*y1);   double lt=0,rt=1e5;   while(dsgn(rt-lt)>0){     double mid=(rt+lt)/2.0;     if(dsgn(2.0*Area(r,mid,l)-pi*r*r)>=0)     rt=mid;     else     lt=mid;   }   printf("%.4lf\n",rt);}return 0;}

0 0
原创粉丝点击