📜  计算梯形高度的程序

📅  最后修改于: 2021-10-23 08:26:54             🧑  作者: Mango

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

例子:

方法:为求梯形的高度,在梯形底边AB上构造垂线DECF ,如下图所示。

现在,三角形 AED 的面积和三角形 CFB 的面积可以使用 Heron 公式计算,边为p1p2(b – a) 。此外,它的面积等于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)

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