AC日记——codeforces Ancient Berland Circus 1c

1C - Ancient Berland Circus

思路:

  求出三角形外接圆;

  然后找出三角形三条边在小数意义下的最大公约数;

  然后n=pi*2/fgcd;

  求出面积即可;

代码:

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

#define INF (1e9)
#define eps (1e-4)
#define pi (3.1415926535)

struct point
{
    double x,y;
};
struct point ai[4];

struct line {
    double k,b;
    
    line *another;
    
    void updata(struct point a,struct point bb)
    {
        another=new line;
        if(a.x==bb.x) k=INF,b=a.x;
        else
        {
            if(a.y==bb.y) k=0,b=a.y;
            else
            {
                k=(bb.y-a.y)/(bb.x-a.x);
                b=a.y-k*a.x;
            }
        }
        if(k==0)
        {
            another->k=INF,another->b=(a.x+bb.x)/2.0;
        }
        else if(k==INF)
        {
            another->k=0,another->b=(a.y+bb.y)/2.0;
        }
        else
        {
            another->k=-1.0/k;
            struct point temp;
            temp.x=(a.x+bb.x)/2.0;
            temp.y=(a.y+bb.y)/2.0;
            another->b=temp.y-another->k*temp.x;
        }
    }
};
struct line bi[4],ci[4];

struct point getnode(line a,line b)
{
    struct point res;
    res.x=(b.b-a.b)/(a.k-b.k);
    res.y=a.k*res.x+a.b;
    return res;
}

double dist(struct point a,struct point b)
{
    double aa=(a.x-b.x)*(a.x-b.x);
    double bb=(a.y-b.y)*(a.y-b.y);
    aa=sqrt(aa+bb);
    return aa;
}

double fgcd(double x, double y) {
    while (fabs(x)>eps&&fabs(y)>eps)
    {
        if (x > y) x-=floor(x/y)*y;
        else y-=floor(y/x)*x;
    }
    return x + y;
}

int main()
{
    for(int i=1;i<=3;i++) cin>>ai[i].x>>ai[i].y;
    bi[1].updata(ai[1],ai[2]);
    bi[2].updata(ai[1],ai[3]);
    bi[3].updata(ai[2],ai[3]);
    for(int i=1;i<=3;i++) ci[i]=*bi[i].another;
    struct point o=getnode(ci[1],ci[2]);
    double R=dist(o,ai[1]);
    double vi[4];
    vi[1]=dist(ai[1],ai[2]);
    vi[2]=dist(ai[1],ai[3]);
    vi[3]=dist(ai[2],ai[3]);
    sort(vi+1,vi+4);
    double an[4];
    an[1]=2.0*asin(vi[1]/(2.0*R));
    an[2]=2.0*asin(vi[2]/(2.0*R));
    an[3]=2.0*pi-an[1]-an[2];
    sort(an+1,an+4);
    double ans1=fgcd(an[2],an[1]);
    double ans2=fgcd(an[3],an[2]);
    double ans=fgcd(max(ans1,ans2),min(ans1,ans2));
    double ans_=(2.0*pi-ans)/2.0;
    printf("%.9lf",R*R*sin(ans)*pi/ans);
    return 0;
}
原文地址:https://www.cnblogs.com/IUUUUUUUskyyy/p/6891535.html