1. 程式人生 > >HDU 4386(計算幾何+婆羅摩笈多公式一般形式)

HDU 4386(計算幾何+婆羅摩笈多公式一般形式)

問題描述:

 One day the little Jack is playing a game with four crabsticks. The game is simple, he want to make all the four crabsticks to be a quadrilateral, which has the biggest area in all the possible ways. But Jack’s math is so bad, he doesn’t know how to do it, can you help him using your excellent programming skills? 

Input

 The first line contains an integer N (1 <= N <= 10000) which indicates the number of test cases. The next N lines contain 4 integers a, b, c, d, indicating the length of the crabsticks.(1 <= a, b, c, d <= 1000) 

Output

 For each test case, please output a line “Case X: Y”. X indicating the number of test cases, and Y indicating the area of the quadrilateral Jack want to make. Accurate to 6 digits after the decimal point. If there is no such quadrilateral, print “-1” instead. 

Sample Input

2
1 1 1 1
1 2 3 4
Sample Output
Case 1: 1.000000
Case 2: 4.898979

題目題意:題目給我們四條邊長,問可不可以構成四邊形,如果可以那麼請輸出最大的四邊形的面積,不行輸出-1。

題目分析:四邊形存在的充要條件是最大的邊長小於其他三邊之和。如果存在四邊形怎麼求四邊形的最大面積了。

現在給出四邊形面積的一般性結論,對於任意四邊形它的面積:


不難發現,我們要四邊形的面積最大,即A等於90°,那麼此時的四邊形是一個圓內接四邊形

程式碼如下:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cmath>
#include<algorithm>
using namespace std;

int a[4];
int main()
{
    int t,icase=1;
    scanf("%d",&t);
    while (t--) {
        scanf("%d%d%d%d",&a[0],&a[1],&a[2],&a[3]);
        sort (a,a+4);
        if (a[3]>=(a[1]+a[2]+a[0])) {
            printf("Case %d: -1\n",icase++);
            continue;
        }
        else {
            double s=(double)(a[0]+a[1]+a[2]+a[3])/2.0;
            double area=sqrt((s-a[0])*(s-a[1])*(s-a[2])*(s-a[3]));
            printf("Case %d: %.6f\n",icase++,area);
        }
    }
    return 0;
}