📜  使用算术平均值和几何平均值找到调和平均值

📅  最后修改于: 2021-04-29 18:04:00             🧑  作者: Mango

给定两个数字,首先计算这两个数字的算术平均值和几何平均值。使用这样计算出的算术平均值和几何平均值,找到两个数字之间的调和平均值。

例子:

Input : a = 2
        b = 4
Output : 2.666

Input : a = 5
        b = 15
Output : 7.500

算术平均值:两个数字a和b之间的算术平均值’AM’等于AM-a = b-AM的数字。因此,如果我们给了这两个数字,则算术平均值AM = 1/2(a + b)
几何均值:两个数字a和b之间的几何均值’GM’等于GM / a = b / GM。因此,如果给定这两个数字,则几何平均值GM = sqrt(a * b)
谐波均值:两个数字a和b之间的谐波均值’HM’等于1 / HM – 1 / a = 1 / b – 1 / HM。因此,如果给定这两个数字,则谐波均值HM = 2ab / a + b
现在,我们也知道GM^2 = AM * HM

C++
// C++ implementation of compution of
// arithmetic mean, geometric mean
// and harmonic mean
#include 
using namespace std;
 
// Function to calculate arithmetic
// mean, geometric mean and harmonic mean
double compute(int a, int b)
{
 
    double AM, GM, HM;
 
    AM = (a + b) / 2;
    GM = sqrt(a * b);
    HM = (GM * GM) / AM;
    return HM;
}
 
// Driver function
int main()
{
 
    int a = 5, b = 15;
    double HM = compute(a, b);
    cout << "Harmonic Mean between " << a
          << " and " << b << " is " << HM ;
    return 0;
}


Java
// Java implementation of compution of
// arithmetic mean, geometric mean
// and harmonic mean
import java.io.*;
 
class GeeksforGeeks {
     
    // Function to calculate arithmetic
    // mean, geometric mean and harmonic mean
    static double compute(int a, int b)
    {
 
        double AM, GM, HM;
 
        AM = (a + b) / 2;
        GM = Math.sqrt(a * b);
        HM = (GM * GM) / AM;
        return HM;
    }
     
    // Driver function
    public static void main(String args[])
    {
        int a = 5, b = 15;
        double HM = compute(a, b);
        String str = "";
        str = str + HM;
        System.out.print("Harmonic Mean between " 
                         + a + " and " + b + " is " 
                         + str.substring(0, 5));
    }
}


Python3
# Python 3 implementation of compution
# of arithmetic mean, geometric mean
# and harmonic mean
 
import math
 
# Function to calculate arithmetic
# mean, geometric mean and harmonic mean
def compute( a, b) :
    AM = (a + b) / 2
    GM = math.sqrt(a * b)
    HM = (GM * GM) / AM
    return HM
 
# Driver function
a = 5
b = 15
HM = compute(a, b)
print("Harmonic Mean between " , a,
      " and ", b , " is " , HM )
 
 
# This code is contributed by Nikita Tiwari.


C#
// C# implementation of compution of
// arithmetic mean, geometric mean
// and harmonic mean
using System;
 
class GeeksforGeeks {
     
    // Function to calculate arithmetic
    // mean, geometric mean and harmonic mean
    static double compute(int a, int b)
    {
 
        double AM, GM, HM;
 
        AM = (a + b) / 2;
        GM = Math.Sqrt(a * b);
        HM = (GM * GM) / AM;
        return HM;
    }
     
    // Driver function
    public static void Main()
    {
        int a = 5, b = 15;
        double HM = compute(a, b);
        Console.WriteLine("Harmonic Mean between "
                        + a + " and " + b + " is "
                        +HM);
    }
}
// This code is contributed by mits


PHP


Javascript


输出:

Harmonic Mean between 5 and 15 is 7.500