Reputation: 1145
Given h
, the hypotenuse and s
, the surface area, it is asked to print the sides of the right angle triangle if possible, else print -1
. So here was my approach;
double h,s;
scanf("%lf %lf",&h,&s);
s*=4;
double squaresum=(h*h) + s;
double squarediff=(h*h) - s;
if(squarediff<0)
printf("-1\n");
else
{
double a = sqrt(squaresum)+sqrt(squarediff);
a/=2;
double b = sqrt(squaresum)-sqrt(squarediff);
b/=2;
if(h>=a+b)
printf("-1\n");
else
printf("%.6lf %.6lf %.6lf\n",h,a,b);
}
My approach:
Given s
, if we multiply 4
, then it is 2*a*b
, where a
and b
are the other sides of the triangle. Then I find (a+b)^2
and (a-b)^2
as I have h*h=a^2+b^2
.
It even passed the custom test case:
4
5 6
6 10
258303 89837245228
616153 77878145466
Output:
4.000000 3.000000 5.000000
-1
-1
546189.769984 285168.817674 616153.000000
But the answer is being judged as wrong. I am not able to pick up how the answer could go wrong given 0<=h<=10^9
and 0<=s<=10^12
.
The problem link-
https://www.codechef.com/problems/RIGHTTRI
Upvotes: 2
Views: 168
Reputation: 4796
Maybe I'm wrong, but if I read the Output required :
Output the answer for each test-case in a single line. If it is not possible to find such a triangle, output -1. Otherwise print 3 real numbers corresponding to the lengths of the sides of the triangle sorted in non-decreasing order. Please note that the length of the triangle sides should not differ by more than 0.01 in absolute value from the correct lengths.
Your output is not sorted... I guess non-decreasing order means increasing order... Maybe give it a shot before anything else...
(Edited question according to comment) :
non-decreasing order means you must sort them number from the lowest to the highest, by length :
3.000000 4.000000 5.000000
Mainly the issue with math problems is to understand what they want from you...
Upvotes: 5