HDU 4033

    余弦定理:假设一个三角形的三条边是a,b,c,对应的三个角是A,B,C,那么cos(C)=(a^2+b^2-c^2)/(2*a*b)。这题先二分正多边形的边长,然后利用余弦定理计算出多边形中的点和多边形的顶点构成的三角形的角度和。显然角度和应该是2*pi。如果大于2*pi,说明设想的多边形边长过长;反之锁门设想的多边形边长过短。以此作为二分的依据。

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <memory.h>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <algorithm>
#include <iostream>
#include <sstream>

#define ll __int64

using namespace std;

const double inf = 1e9;
const double eps = 1e-8;
const double pi = acos(-1.0);
double d[105];

int main() {
    int t, n, i, cas = 1;
    double maxl, minl, mid, sum;
    for (scanf("%d", &t); t--; ) {
        scanf("%d", &n);
        for (i = 0; i < n; i++)
            scanf("%lf", d + i);
        d[n] = d[0];
        maxl = inf; minl = 0.0;
        for (i = 0; i < n; i++)
            maxl = min(maxl, d[i] + d[i+1]);
        while (maxl - minl > eps) {
            mid = (maxl + minl) / 2.0;
            sum = 0.0;
            for (i = 0; i < n; i++)
                sum += acos((d[i] * d[i] + d[i+1] * d[i+1] - mid * mid) / (2.0 * d[i] * d[i+1]));
            if (fabs(sum - (2.0 * pi)) < eps) break;
            if (sum > 2.0 * pi) maxl = mid;
            else minl = mid;
        }
        printf("Case %d: ", cas++);
        if (fabs(sum - (2.0 * pi)) < eps) printf("%.3lf\n", mid);
        else printf("impossible\n");
    }
    return 0;
}

你可能感兴趣的:(c)