📌  相关文章
📜  检查给定区域和斜边是否可能为直角三角形

📅  最后修改于: 2021-10-23 09:01:57             🧑  作者: Mango

给定面积和斜边,如果直角三角形可以存在,则目标是打印所有边,否则打印 -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,则根由线性方程公式给出,roots = (-b +- sqrt(D) )/2*a
这些根将等于边的平方,找到平方根就会得到边。

C++
// C++ program to check existence of
// right triangle.
#include 
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.


Python
# 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 = 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 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
= 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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程