给定三个整数A 、 B和C ,它们表示三角形的三个中线的长度,任务是计算三角形的面积。
A median of a triangle is a line segment joining a vertex to the midpoint of the opposite side, thus bisecting that side.
例子:
Input: A = 9, B = 12, C = 15
Output: 72.0
Input: A = 39, B = 42, C = 45
Output: 1008.0
方法:
三角形的面积可以使用以下等式从给定的中线长度计算:
where
下面是上述方法的实现:
C++14
// C++14 program to calculate
// area of a triangle from the
// given lengths of medians
#include
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
输出:
72.0
时间复杂度: O(1)
辅助空间: O(1)