CodeForces 1C Ancient Berland Circus

题目链接: http://codeforces.com/problemset/problem/1/C



题意题目巴拉巴拉说了一通,其实就是给出一个正n边形上的三个点,要你求出这个正n边形的面积,需要n最小


题解

首先可以明确的是:用着三个点可以确定这个正多边形的外接圆

所有可以先求出这个三点所构成的三角形的边长a,b,c,利用海伦公式求出三角形面积S

然后利用R = abc/(4S) ,求出这个外接圆的半径

    

接下来我们可以求出每一条三角形边所对应的圆心角,然后利用求出这三个圆心角A,B,C的最大公约数

这样n = 2*Pi/gcd(A,B,C)


这里有个新的知识点:求浮点数的gcd,需要用到fmod,这里的fgcd需要设置一个eps,eps=1e-2才OK


#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
const double PI = acos(-1.0);
const double eps = 1e-2;
struct Point
{
    double x, y;
};

double Length(Point a, Point b)
{
    return sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}

double fgcd(double a, double b)
{
    if(fabs(a) < eps) return b;
    if(fabs(b) < eps) return a;
    return fgcd(b, fmod(a,b));
}
int main ()
{
    Point a, b, c;
    scanf("%lf %lf %lf %lf %lf %lf", &a.x, &a.y, &b.x, &b.y, &c.x, &c.y);
    double len1 = Length(a,b);
    double len2 = Length(a,c);
    double len3 = Length(b,c);

    double P = (len1+len2+len3)/2;
    double S = sqrt(P*(P-len1)*(P-len2)*(P-len3));
    double R = len1*len2*len3/(4*S);

    double angle1 = 2*asin(len1/(2*R)) * 180 / PI;
    double angle2 = 2*asin(len2/(2*R)) * 180 / PI;
    double angle3 = 360 - angle1 - angle2;

    double e = 360/fgcd(angle1, fgcd(angle2, angle3));
    double angle = 360 / e;
    printf("%.8f\n", sin(angle * PI / 180) * R * R * e / 2);
    return 0;
}


你可能感兴趣的:(codeforces,计算几何)