首页 > 代码库 > HDU4033:Regular Polygon(二分+余弦定理)
HDU4033:Regular Polygon(二分+余弦定理)
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题意:知道正多边形内一个点到各个顶点的距离,求出这个多边形的边长思路:首先,可以将这个点看做圆心,那么正多边形就是这个圆的内接正多边形,通过这个点与各个顶点的连线,形成n个三角形,其角度之和为360,可以用余弦定理去求#include <stdio.h> #include <string.h> #include <math.h> #include <algorithm> using namespace std; #define pi acos(-1.0) #define exp 1e-8 int main() { int t,n,i,j,flag,cas = 1; double a[105]; scanf("%d",&t); while(t--) { scanf("%d",&n); flag = 0; for(i = 0; i<n; i++) scanf("%lf",&a[i]); a[n] = a[0]; double l = 0,r = 20000,mid; for(i = 1; i<=n; i++) { r = min(r,a[i]+a[i-1]); l = max(l,fabs(a[i]-a[i-1])); } while(r-l>exp) { mid = (l+r)/2; double s,sum=0; for(i = 1; i<=n; i++) { s = (a[i]*a[i]+a[i-1]*a[i-1]-mid*mid)/(2.0*a[i]*a[i-1]); sum+=acos(s); } if(fabs(sum-2*pi)<exp) { flag = 1; break; } else if(sum>2*pi) r = mid; else l = mid; } printf("Case %d: ",cas++); if(flag) printf("%.3f\n",mid); else printf("impossible\n"); } return 0; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。