Quadrilateral
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1232 Accepted Submission(s): 523
Problem Description
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
总结:
判断是否能够成三角形:判断两边(较小的)之和是否大于第三边|| 两边(较大的)之差是否小于第三边
求三角形面积: s=sqrt((p-a)*(p-b)*(p-c)) , p=(a+b+c)/2.0 ;
从上面可以推出:
判断是否能够成四边形:判断三边(较小的)之和是否大于第四边
求四边形面积:s=sqrt((p-a)*(p-b)*(p-c)*(p-d)) , p=(a+b+c+d)/2.0 ;
代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
int main()
{
int i,j,t,sum,a[4];
double p,s;
cin>>t;
j=0;
while(t--)
{
sum=0;
j++;
for(i=0;i<4;i++)
cin>>a[i];
sort(a,a+4);
for(i=0;i<3;i++)
sum+=a[i];
cout<<"Case "<<j<<": ";
if(sum>a[3])
{
p=(a[0]+a[1]+a[2]+a[3])/2.0;
s=sqrt((p-a[0])*(p-a[1])*(p-a[2])*(p-a[3]));
printf("%.6lf\n",s);
}
else
cout<<"-1"<<endl;
}
return 0;
}