hdu 4033 Regular Polygon (幾何題 二分答案)

來源:互聯網
上載者:User

 在正多邊形內部有一點,給出該點到多邊形各點的距離,求正多邊形的邊長

從正多邊形的定義出發,各邊相等各角也相等的多邊形,直接二分答案 , 枚舉邊長 , 滿足各邊所對角的和為2*pi的即可能為答案 ,此時再判斷一下多邊型各角是否相等即可,出現impossible的情況有2種 , 一個是二分搜不到滿足條件的邊長 ,一個是搜到邊不滿足角相等,因為三角形2邊確定,則夾角所對邊和夾角都是單調遞增。

#include <cstdio>#include <cmath>const double pi=acos(-1.0);const double eps=1e-6;const int maxn=105;double dis[maxn];int n;double Acos(double a , double b , double c){    return acos((a*a+b*b-c*c)/(2*a*b));}bool Isregular(double x){    double tmp=Acos(x,dis[0],dis[n-1])+Acos(x,dis[0],dis[1]);    for (int i=1 ; i<n ; ++i)    {        //printf("%lf  ==%lf  %lf\n",tmp , Acos(x,dis[i],dis[i-1]))        if(fabs(tmp-Acos(x,dis[i],dis[i-1])-Acos(x,dis[i],dis[i+1]))>eps)return false;    }    return true;}int main (){    int cas;    scanf("%d",&cas);    for (int I=1 ; I<=cas ; ++I)    {        scanf("%d",&n);        for (int i=0 ; i<n ; ++i)        {            scanf("%lf",dis+i);        }        dis[n]=dis[0];        bool flag=false;        double ans;        double low=0.0 ,high=20005.0;        while (fabs(high-low)>eps)        {            double mid=(low+high)/2;            double delta=0.0;            for (int i=0 ; i<n ; ++i)            {                delta += Acos(dis[i] , dis[i+1] , mid);            }            if(fabs(delta-2*pi)<eps)            {                {                    ans=mid;                    flag=true;                    break;                }            }            if(delta<2*pi)low=mid;            else high=mid;        }        if(flag && Isregular(ans))printf("Case %d: %0.3lf\n" , I , ans);        else printf("Case %d: impossible\n",I);    }    return 0;}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.