给定两个整数a和b代表两个底边的长度,两个整数p1和p2代表梯形的非平行边,任务是找到梯形的高度。
A Trapezoid is a convex quadrilateral with at least one pair of parallel sides. The parallel sides are called the bases of the trapezoid and the other two sides which are not parallel are referred to as the legs. There can also be a pair of bases.
In the figure, CD || AB. Therefore, they form the bases, and the other two sides i.e., AD and BC form the legs.
例子:
Input: a = 14, b = 13, p1 = 25, p2 = 10
Output: 11.2
Input: a = 8, b = 16, p1 = 25, p2 = 10
Output: 7.92401
方法:为求梯形的高度,在梯形底边AB上构造垂线DE和CF ,如下图所示。
现在,三角形 AED 的面积和三角形 CFB 的面积可以使用 Heron 公式计算,边为p1 、 p2和(b – a) 。此外,它的面积等于0.5 * (b – a) * h 。
根据这两个方程,计算h的值,即梯形的高度。
Heron’s Formula: Consider a triangle with sides a, b and c, then
Area of triangle, A = (s (s – a)(s – b)(s – c))1/2, where s = (a + b + c)/2
Also, A = 0.5 * base * height
After rearranging the terms, the height of the trapezoid = (A * 2) / Base
下面是上述方法的实现:
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 现场工作专业课程和学生竞争性编程现场课程。