📜  查找直角三角形的尺寸

📅  最后修改于: 2021-04-24 04:12:45             🧑  作者: Mango

给定直角三角形的H(斜边)和A(面积),找到直角三角形的尺寸,使斜边的长度为H,其面积为A。如果不存在这样的三角形,则打印“不可能”。
例子:

Input : H = 10, A = 24
Output : P = 6.00, B = 8.00

Input : H = 13, A = 36
Output : Not Possible

方法:
在寻求精确解之前,让我们做一些与直角三角形的特性有关的数学计算。
假设H =斜边, P =垂直, B =底, A =直角三角形的面积。
我们有一些方程式:

P^2 + B^2 = H^2
P * B = 2 * A
(P+B)^2 = P^2 + B^2 + 2*P*B = H^2 + 4*A
(P+B) = sqrt(H^2 + 4*A)  ----1
(P-B)^2 = P^2 + B^2 - 2*P*B = H^2 - 4*A
mod(P-B) = sqrt(H^2 - 4*A)  ----2
from equation (2) we can conclude that if 
H^2 < 4*A then no solution is possible.

Further from (1)+(2) and (1)-(2) we have :
P = (sqrt(H^2 + 4*A) + sqrt(H^2 - 4*A) ) / 2
B = (sqrt(H^2 + 4*A) - sqrt(H^2 - 4*A) ) / 2

下面是上述方法的实现:

C++
// CPP program to find dimensions of
// Right angled triangle
#include 
using namespace std;
 
// function to calculate dimension
void findDimen(int H, int A)
{
    // P^2+B^2 = H^2
    // P*B = 2*A
    // (P+B)^2 = P^2+B^2+2*P*B = H^2+4*A
    // (P-B)^2 = P^2+B^2-2*P*B = H^2-4*A
    // P+B = sqrt(H^2+4*A)
    // |P-B| = sqrt(H^2-4*A)
 
    if (H * H < 4 * A) {
        cout << "Not Possible\n";
        return;
    }
 
    // sqrt value of H^2 + 4A and H^2- 4A
    double apb = sqrt(H * H + 4 * A);
    double asb = sqrt(H * H - 4 * A);
 
    // Set precision
    cout.precision(2);
 
    cout << "P = " << fixed
         << (apb - asb) / 2.0 << "\n";
    cout << "B = " << (apb + asb) / 2.0;
}
 
// driver function
int main()
{
    int H = 5;
    int A = 6;
    findDimen(H, A);
    return 0;
}


Java
// Java program to find dimensions of
// Right angled triangle
class GFG {
 
    // function to calculate dimension
    static void findDimen(int H, int A)
    {
 
        // P^2+B^2 = H^2
        // P*B = 2*A
        // (P+B)^2 = P^2+B^2+2*P*B = H^2+4*A
        // (P-B)^2 = P^2+B^2-2*P*B = H^2-4*A
        // P+B = sqrt(H^2+4*A)
        // |P-B| = sqrt(H^2-4*A)
        if (H * H < 4 * A) {
            System.out.println("Not Possible");
            return;
        }
 
        // sqrt value of H^2 + 4A and H^2- 4A
        double apb = Math.sqrt(H * H + 4 * A);
        double asb = Math.sqrt(H * H - 4 * A);
 
        System.out.println("P = " + Math.round(((apb - asb) / 2.0) * 100.0) / 100.0);
        System.out.print("B = " + Math.round(((apb + asb) / 2.0) * 100.0) / 100.0);
    }
 
    // Driver function
    public static void main(String[] args)
    {
        int H = 5;
        int A = 6;
 
        findDimen(H, A);
    }
}
 
// This code is contributed by Anant Agarwal.


Python3
# Python code to find dimensions
# of Right angled triangle
 
# importing the math package
# to use sqrt function
from math import sqrt
 
# function to find the dimensions
def findDimen( H, A):
 
    # P ^ 2 + B ^ 2 = H ^ 2
    # P * B = 2 * A
    # (P + B)^2 = P ^ 2 + B ^ 2 + 2 * P*B = H ^ 2 + 4 * A
    # (P-B)^2 = P ^ 2 + B ^ 2-2 * P*B = H ^ 2-4 * A
    # P + B = sqrt(H ^ 2 + 4 * A)
    # |P-B| = sqrt(H ^ 2-4 * A)
    if H * H < 4 * A:
        print("Not Possible")
        return
 
    # sqrt value of H ^ 2 + 4A and H ^ 2- 4A
    apb = sqrt(H * H + 4 * A)
    asb = sqrt(H * H - 4 * A)
     
    # printing the dimensions
    print("P = ", "%.2f" %((apb - asb) / 2.0))
    print("B = ", "%.2f" %((apb + asb) / 2.0))
     
     
# driver code
H = 5 # assigning value to H
A = 6 # assigning value to A
findDimen(H, A) # calliing function
 
# This code is contributed by "Abhishek Sharma 44"


C#
// C# program to find dimensions of
// Right angled triangle
using System;
 
class GFG {
 
    // function to calculate dimension
    static void findDimen(int H, int A)
    {
 
        // P^2+B^2 = H^2
        // P*B = 2*A
        // (P+B)^2 = P^2+B^2+2*P*B = H^2+4*A
        // (P-B)^2 = P^2+B^2-2*P*B = H^2-4*A
        // P+B = sqrt(H^2+4*A)
        // |P-B| = sqrt(H^2-4*A)
        if (H * H < 4 * A) {
            Console.WriteLine("Not Possible");
            return;
        }
 
        // sqrt value of H^2 + 4A and H^2- 4A
        double apb = Math.Sqrt(H * H + 4 * A);
        double asb = Math.Sqrt(H * H - 4 * A);
 
        Console.WriteLine("P = " + Math.Round(
          ((apb - asb) / 2.0) * 100.0) / 100.0);
           
        Console.WriteLine("B = " + Math.Round(
          ((apb + asb) / 2.0) * 100.0) / 100.0);
    }
 
    // Driver function
    public static void Main()
    {
        int H = 5;
        int A = 6;
 
        findDimen(H, A);
    }
}
 
// This code is contributed by vt_m.


PHP


Javascript


输出:

P = 3.00
B = 4.00