Description
WPH has a triangle whose three edges are of length a, b, c.
He has super power to enlarge the edges, but the total length that was enlarged should be no more than l.
He wants to maximize the area of the triangle.
Input
The first line with an integer T, denoting the number of test cases.
Following T lines, each line with 4 integers a, b, c, l
Data Limit:
1<=T<=104, 1<=a,b,c<=106, 0<=l<=106
Output
For each test case output one line with a float number, denoting the area of the triangle.
Your answer will be considered to be right if it has a relative error less than 10-9.
Sample Input
1 2 3 3 3
Sample Output
5.8216152143
Solution:
1 #include <cstdio> 2 #include <cmath> 3 #include <algorithm> 4 5 double area(double a, double b, double c) { 6 double p = (a + b + c) / 2; 7 return sqrt(p*(p - a)*(p - b)*(p - c)); 8 } 9 10 int main() { 11 int T; 12 fscanf(stdin, "%d", &T); 13 while (T--) { 14 int a, b, c, L; 15 fscanf(stdin, "%d%d%d%d", &a, &b, &c, &L); 16 double s[3]; 17 s[0] = a; 18 s[1] = b; 19 s[2] = c; 20 std::sort(s, s + 3); 21 if ((s[0] + s[1] + L) / 2.0 >= s[2]) { 22 double ss = (s[0] + s[1] + s[2] + L) / 3.0; 23 printf("%.10lf\n", area(ss, ss, ss)); 24 } 25 else if (s[0]+L >= s[1]){ 26 double ss = (s[0] + s[1] + L) / 2.0; 27 printf("%.10lf\n", area(ss, ss, s[2])); 28 } 29 else { 30 double ss = s[0] + L; 31 printf("%.10lf\n", area(ss, s[1], s[2])); 32 } 33 } 34 }
时间: 2024-10-10 11:49:16