检查给定区域和斜边是否可能有直角三角形

给定面积和斜边,目标是打印所有边(如果可以存在直角三角形),否则打印-1。我们需要按升序打印所有面。

null

例如:

Input  : 6 5Output : 3 4 5Input  : 10 6Output : -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 <bits/stdc++.h>
using namespace std;
// Prints three sides of a right triangle
// 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 triangle
// 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.


Python3

# Python program to check existence of
# right triangle.
from math import sqrt
# Prints three sides of a right triangle
# 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 = int (sqrt(root1))
b = int (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 triangle
// 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

<?php
// PHP program to check existence of
// right triangle.
// Prints three sides of a right triangle
// from given area and hypotenuse if
// triangle is possible, else prints -1.
function 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 )
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

<script>
// Javascript program to check existence of
// right triangle.
// Prints three sides of a right triangle
// from given area and hypotenuse if triangle
// is possible, else prints -1.
function findRightAngle(A,H)
{
// Descriminant of the equation
let D = Math.pow(H, 4) - 16 * A * A;
if (D >= 0)
{
// applying the linear equation
// formula to find both the roots
let root1 = (H * H + Math.sqrt(D)) / 2;
let root2 = (H * H - Math.sqrt(D)) / 2;
let a = Math.sqrt(root1);
let b = Math.sqrt(root2);
if (b >= a)
document.write(a + " " + b + " " + H+ "<br/>" );
else
document.write(b + " " + a + " " + H+ "<br/>" );
}
else
document.write( "-1" );
}
// Driver code
findRightAngle(6, 5);
// This code contributed by Rajput-Ji
</script>


输出:

3 4 5

本文由 哈希特·阿格拉瓦尔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享