Regular Polygon
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 3274 Accepted Submission(s): 996
Problem Description
In
a 2_D plane, there is a point strictly in a regular polygon with N
sides. If you are given the distances between it and N vertexes of the
regular polygon, can you calculate the length of reguler polygon's side?
The distance is defined as dist(A, B) = sqrt( (Ax-Bx)*(Ax-Bx) +
(Ay-By)*(Ay-By) ). And the distances are given counterclockwise.
Input
First
a integer T (T≤ 50), indicates the number of test cases. Every test
case begins with a integer N (3 ≤ N ≤ 100), which is the number of
regular polygon's sides. In the second line are N float numbers,
indicate the distance between the point and N vertexes of the regular
polygon. All the distances are between (0, 10000), not inclusive.
Output
For
the ith case, output one line “Case k: ” at first. Then for every test
case, if there is such a regular polygon exist, output the side's length
rounded to three digits after the decimal point, otherwise output
“impossible”.
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
Source
已知一个点到正n边形的n个顶点的距离,求正n边形的边长。
思路:
在已知的表达式中,求不出n边形的边长。但是依据两边之和大于第三边,两边之差小鱼第三边。可以得到这个边的范围.
然后由于n边形的以任意一个点,连接到所有顶点,所有的夹角之和为360,所以只需要采取二分依次来判断,是否满足。
代码:
1 #include<cstdio> 2 #include<cstring> 3 #include<cmath> 4 #include<algorithm> 5 #define pi acos(-1.0) 6 #define esp 1e-8 7 using namespace std; 8 double aa[105]; 9 int main() 10 { 11 int cas,n; 12 double rr,ll; 13 scanf("%d",&cas); 14 for(int i=1;i<=cas;i++) 15 { 16 scanf("%d",&n); 17 for(int j=0;j<n;j++) 18 scanf("%lf",aa+j); 19 //确定上下边界 20 ll=20001,rr=0; 21 for(int j=0;j<n;j++) 22 { 23 rr=max(rr,aa[j]+aa[(j+1)%n]); 24 ll=min(ll,fabs(aa[j]-aa[(j+1)%n])); 25 } 26 double mid,sum,cosa; 27 printf("Case %d: ",i); 28 bool tag=0; 29 while(rr>esp+ll) 30 { 31 mid=ll+(rr-ll)/2; 32 sum=0; 33 for(int j=0;j<n;j++){ 34 //oosr=a*a+b*b-mid*mid; 余弦定理求夹角,然后判断所有的夹角之和是否为360 35 cosa=(aa[j]*aa[j]+aa[(j+1)%n]*aa[(j+1)%n]-mid*mid)/(2.0*aa[j]*aa[(j+1)%n]); 36 sum+=acos(cosa); 37 } 38 if(fabs(sum-2*pi)<esp){ 39 tag=1; 40 printf("%.3lf ",mid); 41 break; 42 } 43 else 44 if(sum<2*pi) ll=mid; 45 else 46 rr=mid; 47 } 48 if(tag==0) 49 printf("impossible "); 50 } 51 return 0; 52 }