HDU

来源:互联网 发布:没有网站外链好优化么 编辑:程序博客网 时间:2024/06/06 03:28

Zu Chongzhi (429–500) was a prominent Chinese mathematician and astronomer during the Liu Song and Southern Qi Dynasties. Zu calculated the value ofπ to the precision of six decimal places and for a thousand years thereafter no subsequent mathematician computed a value this precise. Zu calculated one year as 365.24281481 days, which is very close to 365.24219878 days as we know today. He also worked on deducing the formula for the volume of a sphere. 

It is said in some legend story books that when Zu was a little boy, he liked mathematical games. One day, his father gave him some wood sticks as toys. Zu Chongzhi found a interesting problem using them. He wanted to make some triangles by those sticks, and he wanted the total area of all triangles he made to be as large as possible. The rules were : 

1) A triangle could only consist of 3 sticks. 
2) A triangle's vertexes must be end points of sticks. A triangle's vertex couldn't be in the middle of a stick. 
3) Zu didn't have to use all sticks. 

Unfortunately, Zu didn't solve that problem because it was an algorithm problem rather than a mathematical problem. You can't solve that problem without a computer if there are too many sticks. So please bring your computer and go back to Zu's time to help him so that maybe you can change the history.
Input
There are no more than 10 test cases. For each case: 

The first line is an integer N(3 <= N<= 12), indicating the number of sticks Zu Chongzhi had got. The second line contains N integers, meaning the length of N sticks. The length of a stick is no more than 100. The input ends with N = 0.
Output
For each test case, output the maximum total area of triangles Zu could make. Round the result to 2 digits after decimal point. If Zu couldn't make any triangle, print 0.00 .
Sample Input
31 1 2073 4 5 3 4 5 900
Sample Output
0.0013.64

题意:

给你最多12条边,让你组成三角形,可以组成多个三角形,问你所有能组成的三角形的面积的最大值是多少。


思路:

刚开始想的是瞎暴力,每三条边组成一个三角形算一遍,然后想了想不可取,一是不好实现,二是复杂度是12!,显然超时。

队友想了个法:先把边按照从大到小的顺序排个序,排好之后从头来,每三条边组成一个三角形,如果组成不了三角形,就舍去最大的那个

这个想法很好啊,但是刚开始我觉得不严谨,后来想了想还是有一定的道理的

好比是三个大牛和三个弱鸡,两个大牛和一个弱鸡一起,战斗力都被弱鸡搞下去了(两长边一短边,面积小的可怜)

而另外一边,俩弱鸡和一个大牛,大牛根本带不动这俩弱鸡(长边太长,组成不了三角形)

那么比较好的办法就是能组成大的面积的就让他组,这样总面积能最大。(三个大牛在一起战斗力爆表,三个弱鸡就算战斗力低,也能有点贡献)


#include <iostream>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <algorithm>#include <math.h>#include <vector>#include <iostream>#include <cstdio>double getS(int a,int b,int c){    double d=(double)(a*a+b*b-c*c)/(2.0*a*b);    double e=sqrt(1-d*d);    double ans=0.5*a*b*e;    return ans;}using namespace std;int a[15];int main(){    int n;    //cout<<getS(5,5,4)+getS(4,3,3);    while(scanf("%d",&n)&&n)    {        for(int i=0;i<n;i++)            scanf("%d",&a[i]);            sort(a,a+n,greater<int>());        double sum=0;       for(int i=0;i<n-2;i++)            {                if((a[i]+a[i+1])>a[i+2]&&(a[i]+a[i+2])>a[i+1]&&(a[i+1]+a[i+2])>a[i])                {                    sum+=getS(a[i],a[i+1],a[i+2]);                    //cout<<"sum=  "<<getS(a[i],a[i+1],a[i+2])<<endl;                    i+=2;                }            }            //cout<<sum<<endl;        printf("%.2f\n",sum);    }    return 0;}