📜  如果给出边的长度,则求出三角形的中位数的长度

📅  最后修改于: 2021-04-29 13:49:44             🧑  作者: Mango

给定一个三角形的所有三个边的长度为abc 。任务是计算三角形中位数的长度。

例子:

方法:想法是使用Apollonius定理解决该问题。

因此,根据上述等式,三角形的中值长度由下式给出:

下面是上述方法的实现:

C++
// C++ program to find the length of the 
// median using sides of the triangle
#include
using namespace std;
  
// Function to return the length of
// the median using sides of triangle
float median(int a, int b, int c)
{
    float n = sqrt(2 * b * b + 
                   2 * c * c - a * a) / 2;
    return n;
}
  
// Driver code
int main()
{
    int a, b, c;
    a = 4;
    b = 3;
    c = 5;
  
    // Function call
    float ans = median(a, b, c); 
  
    // Print final answer with 2 
    // digits after decimal
    cout << fixed << setprecision(2) << ans;
    return 0;
}
  
// This code is contributed by himanshu77


Java
// Java program to find the length of the 
// median using sides of the triangle 
import java.util.*; 
  
class GFG{
      
// Function to return the length of 
// the median using sides of triangle 
public static float median(int a, int b, int c) 
{ 
    float n = (float)(Math.sqrt(2 * b * b + 
                                2 * c * c - 
                                a * a) / 2); 
    return n; 
} 
  
// Driver code
public static void main(String[] args)
{
    int a, b, c; 
    a = 4; 
    b = 3; 
    c = 5; 
  
    // Function call 
    float ans = median(a, b, c); 
  
    // Print final answer with 2 
    // digits after decimal 
    System.out.println(String.format("%.2f", ans)); 
}
}
  
// This code is contributed by divyeshrabadiya07


Python3
# Python3 implementation to Find the 
# length of the median using sides
# of the triangle
  
import math
  
# Function to return the length of 
# the median using sides of triangle. 
def median(a, b, c): 
   
    n = (1 / 2)*math.sqrt(2*(b**2) 
   + 2*(c**2)
 - a**2)  
  
    return n 
  
# Driver Code 
a = 4
b = 3
c = 5
  
# Function Call
ans = median(a, b, c)
  
# Print the final answer
print(round(ans, 2))


C#
// C# program to find the length of the 
// median using sides of the triangle 
using System;
  
class GFG{
      
// Function to return the length of 
// the median using sides of triangle 
public static float median(int a, int b, int c) 
{ 
    float n = (float)(Math.Sqrt(2 * b * b + 
                                2 * c * c - 
                                a * a) / 2); 
    return n; 
} 
  
// Driver code
public static void Main(String[] args)
{
    int a, b, c; 
    a = 4; 
    b = 3; 
    c = 5; 
  
    // Function call 
    float ans = median(a, b, c); 
  
    // Print readonly answer with 2 
    // digits after decimal 
    Console.WriteLine(String.Format("{0:F2}", ans)); 
}
}
  
// This code is contributed by gauravrajput1


输出:
3.61

时间复杂度: O(1)
空间复杂度: O(1)