程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> HDU4033:Regular Polygon(二分+余弦定理)

HDU4033:Regular Polygon(二分+余弦定理)

編輯:C++入門知識

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 
#include 
#include 
#include 
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; iexp)
        {
            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)2*pi)
                r = mid;
            else
                l = mid;
        }
        printf("Case %d: ",cas++);
        if(flag)
            printf("%.3f\n",mid);
        else
            printf("impossible\n");
    }

    return 0;
}


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved