Dados tres números enteros A , B y C que denotan la longitud de las tres medianas de un triángulo, la tarea es calcular el área del triángulo.
La mediana de un triángulo es un segmento de recta que une un vértice con el punto medio del lado opuesto, bisecando así ese lado.
Ejemplos:
Entrada: A = 9, B = 12, C = 15
Salida: 72,0
Entrada: A = 39, B = 42, C = 45
Salida: 1008,0
Enfoque:
el área del triángulo se puede calcular a partir de la longitud dada de las medianas usando la siguiente ecuación:
dónde
A continuación se muestra la implementación del enfoque anterior:
C++14
// C++14 program to calculate // area of a triangle from the // given lengths of medians #include <bits/stdc++.h> using namespace std; // Function to return the area of // triangle using medians double Area_of_Triangle(int a, int b, int c) { int s = (a + b + c) / 2; int x = s * (s - a); x = x * (s - b); x = x * (s - c); double area = (4 / (double)3) * sqrt(x); return area; } // Driver Code int main() { int a = 9; int b = 12; int c = 15; // Function call double ans = Area_of_Triangle(a, b, c); // Print the final answer cout << ans; } // This code is contributed by code_hunt
Java
// Java program to calculate // area of a triangle from the // given lengths of medians class GFG{ // Function to return the area of // triangle using medians static double Area_of_Triangle(int a, int b, int c) { int s = (a + b + c)/2; int x = s * (s - a); x = x * (s - b); x = x * (s - c); double area = (4 / (double)3) * Math.sqrt(x); return area; } // Driver Code public static void main(String[] args) { int a = 9; int b = 12; int c = 15; // Function Call double ans = Area_of_Triangle(a, b, c); // Print the final answer System.out.println(ans); } } // This code is contributed by sapnasingh4991
Python3
# Python3 program to calculate # area of a triangle from the # given lengths of medians import math # Function to return the area of # triangle using medians def Area_of_Triangle(a, b, c): s = (a + b + c)//2 x = s * (s - a) x = x * (s - b) x = x * (s - c) area = (4 / 3) * math.sqrt(x) return area # Driver Code a = 9 b = 12 c = 15 # Function Call ans = Area_of_Triangle(a, b, c) # Print the final answer print(round(ans, 2))
C#
// C# program to calculate // area of a triangle from the // given lengths of medians using System; class GFG{ // Function to return the area of // triangle using medians static double Area_of_Triangle(int a, int b, int c) { int s = (a + b + c) / 2; int x = s * (s - a); x = x * (s - b); x = x * (s - c); double area = (4 / (double)3) * Math.Sqrt(x); return area; } // Driver Code public static void Main(String[] args) { int a = 9; int b = 12; int c = 15; // Function call double ans = Area_of_Triangle(a, b, c); // Print the final answer Console.WriteLine(ans); } } // This code is contributed by sapnasingh4991
Javascript
<script> // javascript program to calculate // area of a triangle from the // given lengths of medians // Function to return the area of // triangle using medians function Area_of_Triangle(a , b , c) { var s = (a + b + c) / 2; var x = s * (s - a); x = x * (s - b); x = x * (s - c); var area = (4 / 3) * Math.sqrt(x); return area; } // Driver Code var a = 9; var b = 12; var c = 15; // Function Call var ans = Area_of_Triangle(a, b, c); // Print the final answer document.write(ans.toFixed(1)); // This code is contributed by Rajput-Ji </script>
72.0
Tiempo Complejidad: O(1)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por divyamohan123 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA