UVALive 7077 - Little Zu Chongzhi's Triangles(暴力)

https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=5089

题目大意:给n个木棒,用这n个木棒组成多个三角形,求这些三角形面积和的最大值,如果一个三角也不能组成则输出0.00

(注意:一根木棒就可以当做一条边,刚开始就错误的以为一条边可以由很多木棒共同组成而将题想复杂了)

分析:

将这n个木棒按长度从大到小排(从小到大票排是错误的,我也不知道为什么是错的--!)然后用双重循环暴力,找三角形的三

条边,利用海伦公式求三角形的面积

海伦公式:

a, b, c 分别为三角形的三条边,s为三角形的面积

p = (a + b + c)/2

s = sqrt(p(p-a)(p-b)(p-c));

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<algorithm>

using namespace std;

const int N = 30;
int d[N];

int cmp(const void *a, const void *b)
{
    return *(int *)b - *(int *)a;
}

bool judge(int a, int b, int c)
{
    if(a + b > c && a + c > b && b + c > a)
        return true;
    return false;
}

int main()
{
    int n, i, j, a, b, c, f;
    double s, sum, p;
    while(scanf("%d", &n), n)
    {
        sum = 0;
        for(i = 0 ; i < n ; i++)
            scanf("%d", &d[i]);
        qsort(d, n, sizeof(d[0]), cmp);
        i = 0;
        while(i < n - 1)
        {
            f = 0;
            j = i + 1;
            while(j < n)
            {
                if(judge(d[i], d[j], d[j + 1]))
                {
                    a = d[i];
                    b = d[j];
                    c = d[j + 1];
                    p = 1.0 * (a + b + c) / 2;
                    s = 1.0 * sqrt(p * (p - a) * (p - b) * (p - c));
                    sum += s;
                    i += 3;
                    j = n;
                    f = 1;
                }
                else
                    j++;
            }
            if(f == 0)
                i++;
        }
        if(sum == 0)
            printf("0.00\n");
        else
            printf("%.2f\n", sum);
    }
    return 0;
}

 

posted @ 2015-10-09 19:07  午夜阳光~  阅读(255)  评论(0编辑  收藏  举报