给定两个整数 a 和 b,找到最小可能的高度,使得可以形成至少面积“a”和底面“b”的三角形。
例子 :
Input : a = 2, b = 2
Output : Minimum height of triangle is 2
Explanation:
Input : a = 8, b = 4
Output : Minimum height of triangle is 4
可以通过了解三者之间的关系来评估底为“b”和面积为“a”的三角形的最小高度。
The relation between area, base and
height is:
area = (1/2) * base * height
So height can be calculated as :
height = (2 * area)/ base
Minimum height is the ceil of the
height obtained using above formula.
C++
#include
using namespace std;
// function to calculate minimum height of
// triangle
int minHeight(int base, int area){
return ceil((float)(2*area)/base);
}
int main() {
int base = 4, area = 8;
cout << "Minimum height is "
<< minHeight(base, area) << endl;
return 0;
}
Java
// Java code Minimum height of a
// triangle with given base and area
class GFG {
// function to calculate minimum height of
// triangle
static double minHeight(double base, double area)
{
double d = (2 * area) / base;
return Math.ceil(d);
}
// Driver code
public static void main (String[] args)
{
double base = 4, area = 8;
System.out.println("Minimum height is "+
minHeight(base, area));
}
}
// This code is contributed by Anant Agarwal.
Python
# Python Program to find minimum height of triangle
import math
def minHeight(area,base):
return math.ceil((2*area)/base)
# Driver code
area = 8
base = 4
height = minHeight(area, base)
print("Minimum height is %d" % (height))
C#
// C# program to find minimum height of
// a triangle with given base and area
using System;
public class GFG {
// function to calculate minimum
// height of triangle
static int minHeight(int b_ase, int area)
{
return (int)Math.Round((float)(2 * area) / b_ase);
}
// Driver function
static public void Main()
{
int b_ase = 4, area = 8;
Console.WriteLine("Minimum height is "
+ minHeight(b_ase, area));
}
}
// This code is contributed by vt_m.
PHP
Javascript
输出 :
Minimum height is 4
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。