前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >hdu 4033Regular Polygon(二分+余弦定理)

hdu 4033Regular Polygon(二分+余弦定理)

作者头像
Gxjun
发布2018-03-26 15:59:58
6350
发布2018-03-26 15:59:58
举报
文章被收录于专栏:ml

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

The 36th ACM/ICPC Asia Regional Chengdu Site —— Online Contest

  已知一个点到正n边形的n个顶点的距离,求正n边形的边长。

思路:

       在已知的表达式中,求不出n边形的边长。但是依据两边之和大于第三边,两边之差小鱼第三边。可以得到这个边的范围.

   然后由于n边形的以任意一个点,连接到所有顶点,所有的夹角之和为360,所以只需要采取二分依次来判断,是否满足。

代码:

代码语言:javascript
复制
 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\n",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\n");
50   }
51   return 0;
52 }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2014-10-20 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Regular Polygon
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档