Dados dos números enteros a y b, encuentre la altura más pequeña posible tal que se pueda formar un triángulo de al menos área «a» y base «b».
Ejemplos:
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
La altura mínima del Triángulo con base “b” y área “a” se puede evaluar conociendo la relación entre los tres.
La relación entre el área, la base y la
altura es:
área = (1/2) * base * altura
Por lo tanto, la altura se puede calcular como:
altura = (2 * área)/ base
La altura mínima es el techo de la
altura obtenida usando la fórmula anterior.
C++
#include <bits/stdc++.h> 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
<?php // function to calculate minimum // height of triangle function minHeight($base, $area) { return ceil((2 * $area) / $base); } // Driver Code $base = 4;$area = 8; echo "Minimum height is ", minHeight($base, $area); // This code is contributed by anuj_67. ?>
Javascript
<script> // function to calculate minimum height of // triangle function minHeight( base, area){ return Math.ceil((2*area)/base); } let base = 4, area = 8; document.write( "Minimum height is " +minHeight(base, area)); // This code contributed by aashish1995 </script>
Producción :
Minimum height is 4
Complejidad temporal: O(1)
Espacio auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por shubham_rana_77 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA