📜  查找梯形高度的程序

📅  最后修改于: 2021-04-17 16:50:47             🧑  作者: Mango

给定两个整数ab代表两个底的长度,两个整数p1p2代表梯形的不平行边,任务是找到梯形的高度。

例子:

方法:要找到梯形的高度,请在梯形的底部AB上构造垂直的DECF ,如下图所示。

现在,可以使用带有面p1p2(b – a)的Heron公式来计算三角形AED的面积和三角形CFB的面积。而且,其面积等于0.5 *(b – a)* h
根据这两个方程,计算h的值,它是梯形的高度。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Function to calculate height
// of the trapezoid
void findHeight(float p1, float p2,
                float b, float c)
{
    float a = max(p1, p2) - min(p1, p2);
 
    // Apply Heron's formula
    float s = (a + b + c) / 2;
 
    // Calculate the area
    float area = sqrt(s * (s - a)
                      * (s - b) * (s - c));
 
    // Calculate height of trapezoid
    float height = (area * 2) / a;
 
    // Print the height
    cout << "Height is: " << height;
}
 
// Driver Code
int main()
{
    // Given a, b, p1 and p2
    float p1 = 25, p2 = 10;
    float a = 14, b = 13;
 
    findHeight(p1, p2, a, b);
 
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.*;
 
class GFG
{
   
// Function to calculate height
// of the trapezoid
static void findHeight(float p1, float p2,
                float b, float c)
{
    float a = Math.max(p1, p2) - Math.min(p1, p2);
 
    // Apply Heron's formula
    float s = (a + b + c) / 2;
 
    // Calculate the area
    float area = (int)Math.sqrt(s * (s - a)
                      * (s - b) * (s - c));
 
    // Calculate height of trapezoid
    float height = (area * 2) / a;
 
    // Print the height
    System.out.print("Height is: " + height);
}
 
// Driver Code
public static void main(String args[])
{
   
    // Given a, b, p1 and p2
    float p1 = 25, p2 = 10;
    float a = 14, b = 13;
 
    findHeight(p1, p2, a, b);
}
}
 
// This code is contributed by splevel62.


Python3
# Python3 program to implement
# the above approach
import math
 
# Function to calculate height
# of the trapezoid
def findHeight(p1, p2, b, c) :            
    a = max(p1, p2) - min(p1, p2)
 
    # Apply Heron's formula
    s = (a + b + c) // 2
 
    # Calculate the area
    area = math.sqrt(s * (s - a)
                      * (s - b) * (s - c))
 
    # Calculate height of trapezoid
    height = (area * 2) / a
 
    # Prthe height
    print("Height is: ",  height)
 
 
# Driver Code
 
# Given a, b, p1 and p2
p1 = 25
p2 = 10
a = 14
b = 13
 
findHeight(p1, p2, a, b)
 
# this code is contributed by sanjoy_62.


C#
// C# program to implement
// the above approach
using System;
 
public class GFG
{
   
// Function to calculate height
// of the trapezoid
static void findHeight(float p1, float p2,
                float b, float c)
{
    float a = Math.Max(p1, p2) - Math.Min(p1, p2);
 
    // Apply Heron's formula
    float s = (a + b + c) / 2;
 
    // Calculate the area
    float area = (int)Math.Sqrt(s * (s - a)
                      * (s - b) * (s - c));
 
    // Calculate height of trapezoid
    float height = (area * 2) / a;
 
    // Print the height
    Console.Write("Height is: " + height);
}
 
// Driver Code
public static void Main(String []args)
{
   
    // Given a, b, p1 and p2
    float p1 = 25, p2 = 10;
    float a = 14, b = 13;
 
    findHeight(p1, p2, a, b);
}
}
 
// This code is contributed by shikhasingrajput


输出:
Height is: 11.2

时间复杂度: O(1)
辅助空间: O(1)