给定面积和斜边,目标是如果可以存在直角三角形,则打印所有侧面,否则打印-1。我们需要按升序打印所有面。
例子:
Input : 6 5
Output : 3 4 5
Input : 10 6
Output : -1
我们在下面的文章中讨论了这个问题的解决方案。
从给定的斜边和面积中找到直角三角形的所有边|套装1
在这篇文章中,将讨论具有以下逻辑的新解决方案。
设两个未知面为a和b
面积:A = 0.5 * a * b
斜边正方形:H ^ 2 = a ^ 2 + b ^ 2
代入b,我们得到H 2 = a 2 +(4 * A 2 )/ a 2
重新排列后,我们得到等式a 4 –(H 2 )(a 2 )+ 4 *(A 2 )
该方程的判别式D为D = H 4 – 16 *(A 2 )
如果D = 0,则根由线性方程式给出,根=(-b +-sqrt(D))/ 2 * a
这些根将等于边的平方,找到平方根将给我们边。
C++
// C++ program to check existence of
// right triangle.
#include
using namespace std;
// Prints three sides of a right trianlge
// from given area and hypotenuse if triangle
// is possible, else prints -1.
void findRightAngle(int A, int H)
{
// Descriminant of the equation
long D = pow(H, 4) - 16 * A * A;
if (D >= 0)
{
// applying the linear equation
// formula to find both the roots
long root1 = (H * H + sqrt(D)) / 2;
long root2 = (H * H - sqrt(D)) / 2;
long a = sqrt(root1);
long b = sqrt(root2);
if (b >= a)
cout << a << " " << b << " " << H;
else
cout << b << " " << a << " " << H;
}
else
cout << "-1";
}
// Driver code
int main()
{
findRightAngle(6, 5);
}
// This code is contributed By Anant Agarwal.
Java
// Java program to check existence of
// right triangle.
class GFG {
// Prints three sides of a right trianlge
// from given area and hypotenuse if triangle
// is possible, else prints -1.
static void findRightAngle(double A, double H)
{
// Descriminant of the equation
double D = Math.pow(H, 4) - 16 * A * A;
if (D >= 0)
{
// applying the linear equation
// formula to find both the roots
double root1 = (H * H + Math.sqrt(D)) / 2;
double root2 = (H * H - Math.sqrt(D)) / 2;
double a = Math.sqrt(root1);
double b = Math.sqrt(root2);
if (b >= a)
System.out.print(a + " " + b + " " + H);
else
System.out.print(b + " " + a + " " + H);
}
else
System.out.print("-1");
}
// Driver code
public static void main(String arg[])
{
findRightAngle(6, 5);
}
}
// This code is contributed by Anant Agarwal.
Python
# Python program to check existence of
# right triangle.
from math import sqrt
# Prints three sides of a right trianlge
# from given area and hypotenuse if triangle
# is possible, else prints -1.
def findRightAngle(A, H):
# Descriminant of the equation
D = pow(H,4) - 16 * A * A
if D >= 0:
# applying the linear equation
# formula to find both the roots
root1 = (H * H + sqrt(D))/2
root2 = (H * H - sqrt(D))/2
a = sqrt(root1)
b = sqrt(root2)
if b >= a:
print a, b, H
else:
print b, a, H
else:
print "-1"
# Driver code
# Area is 6 and hypotenuse is 5.
findRightAngle(6, 5)
C#
// C# program to check existence of
// right triangle.
using System;
class GFG {
// Prints three sides of a right trianlge
// from given area and hypotenuse if triangle
// is possible, else prints -1.
static void findRightAngle(double A, double H)
{
// Descriminant of the equation
double D = Math.Pow(H, 4) - 16 * A * A;
if (D >= 0) {
// applying the linear equation
// formula to find both the roots
double root1 = (H * H + Math.Sqrt(D)) / 2;
double root2 = (H * H - Math.Sqrt(D)) / 2;
double a = Math.Sqrt(root1);
double b = Math.Sqrt(root2);
if (b >= a)
Console.WriteLine(a + " " + b + " " + H);
else
Console.WriteLine(b + " " + a + " " + H);
}
else
Console.WriteLine("-1");
}
// Driver code
public static void Main()
{
findRightAngle(6, 5);
}
}
// This code is contributed by vt_m.
PHP
= 0)
{
// applying the linear equation
// formula to find both the roots
$root1 = ($H * $H + sqrt($D)) / 2;
$root2 = ($H * $H - sqrt($D)) / 2;
$a = sqrt($root1);
$b = sqrt($root2);
if ($b >= $a)
echo $a , " ", $b , " " , $H;
else
echo $b , " " , $a , " " , $H;
}
else
echo "-1";
}
// Driver code
findRightAngle(6, 5);
// This code is contributed By Anuj_67
?>
Javascript
输出:
3 4 5