2 3 3.0 4.0 5.0 3 1.0 2.0 3.0
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; }
HDU4033:Regular Polygon(二分+余弦定理),布布扣,bubuko.com
HDU4033:Regular Polygon(二分+余弦定理)
原文地址:http://blog.csdn.net/libin56842/article/details/26618129