参考:http://blog.csdn.net/libin56842/article/details/26618129
题意:给一个正多边形内点到其他顶点的距离(逆时针给出),求正多边形的边长
二分多边形的边长
根据余弦定理求出A的角度,之后求出所有角度,加起来是否为360,小于则扩大,大于则缩小边
Sample Input
2
3
3.0 4.0 5.0
3
1.0 2.0 3.0
Sample Output
Case 1: 6.766
Case 2: impossible
1 #include <stdio.h> 2 #include <string.h> 3 #include <math.h> 4 #include <algorithm> 5 using namespace std; 6 #define pi acos(-1.0) 7 #define exp 1e-8 8 int main() 9 { 10 int t,n,i,j,flag,cas = 1; 11 double a[105]; 12 scanf("%d",&t); 13 while(t--) 14 { 15 scanf("%d",&n); 16 flag = 0; 17 for(i = 0; i<n; i++) 18 scanf("%lf",&a[i]); 19 a[n] = a[0]; 20 double l = 0,r = 20000,mid; 21 for(i = 1; i<=n; i++) 22 { 23 r = min(r,a[i]+a[i-1]); 24 l = max(l,fabs(a[i]-a[i-1])); 25 } 26 while(r-l>exp) 27 { 28 mid = (l+r)/2; 29 double s,sum=0; 30 for(i = 1; i<=n; i++) 31 { 32 s = (a[i]*a[i]+a[i-1]*a[i-1]-mid*mid)/(2.0*a[i]*a[i-1]); 33 sum+=acos(s); 34 } 35 if(fabs(sum-2*pi)<exp) 36 { 37 flag = 1; 38 break; 39 } 40 else if(sum>2*pi) 41 r = mid; 42 else 43 l = mid; 44 } 45 printf("Case %d: ",cas++); 46 if(flag) 47 printf("%.3f\n",mid); 48 else 49 printf("impossible\n"); 50 } 51 52 return 0; 53 }