📜  调和数均值的程序

📅  最后修改于: 2021-04-23 08:15:08             🧑  作者: Mango

给定一个元素数组,找出数字的调和均值。
例子:

Input : arr[] = {2.0, 1.0}
Output : 1.3333
Harmonic mean = 2/(1/2.0 + 1/1.0)
             = (2 * 2)/3
             = 1.333

Input : arr[] = {13.5, 14.5, 14.8, 15.2, 16.1}
Output : 14.7707

以下是谐波均值的实现。

C++
// CPP program to find harmonic mean of numbers.
#include 
using namespace std;
 
// Function that returns harmonic mean.
float harmonicMean(float arr[], int n)
{
    // Declare sum variables and initialize
    // with zero.
    float sum = 0;
    for (int i = 0; i < n; i++)
        sum = sum + (float)1 / arr[i];
 
    return (float)n/sum;
}
 
// Driver code
int main()
{
    float arr[] = { 13.5, 14.5, 14.8, 15.2, 16.1 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << harmonicMean(arr, n);
    return 0;
}


Java
// JAVA program to find harmonic
// mean of numbers.
import java.io.*;
 
class GFG {
     
    // Function that returns harmonic mean.
    static float harmonicMean(float arr[], int n)
    {
        // Declare sum variables and
        // initialize with zero
        float sum = 0;
        for (int i = 0; i < n; i++)
            sum = sum + (float)1 / arr[i];
      
        return (float)n/sum;
    }
      
    // Driver code
    public static void main(String args[])
    {
        float arr[]= { 13.5f, 14.5f, 14.8f,
                      15.2f, 16.1f };
        int n = arr.length;
        System.out.println(harmonicMean(arr, n));
    }
}
 
/*This code is contributed by Nikita Tiwari.*/


Python3
# Python 3 program to find harmonic
# mean of numbers.
 
# Function that returns harmonic mean.
def harmonicMean(arr, n) :
   
    # Declare sum variables and
    # initialize with zero.
    sm = 0
    for i in range(0, n) :
        sm = sm + (1) / arr[i];
  
    return n / sm
 
 
# Driver code
arr = [ 13.5, 14.5, 14.8, 15.2, 16.1 ];
n = len(arr)
print(harmonicMean(arr, n))
 
# This code is contributed by Nikita Tiwari.


C#
// C# program to find harmonic
// mean of numbers.
using System;
 
class GFG {
 
    // Function that returns harmonic mean.
    static float harmonicMean(float[] arr, int n)
    {
        // Declare sum variables and
        // initialize with zero
        float sum = 0;
        for (int i = 0; i < n; i++)
            sum = sum + (float)1 / arr[i];
 
        return (float)n / sum;
    }
 
    // Driver code
    public static void Main()
    {
        float[] arr = { 13.5f, 14.5f, 14.8f,
                        15.2f, 16.1f };
        int n = arr.Length;
        Console.WriteLine(harmonicMean(arr, n));
    }
}
 
/*This code is contributed by vt_m.*/


PHP


Javascript


C++
// C++ program to find harmonic mean.
#include 
using namespace std;
// Function that returns harmonic mean.
float harmonicMean(int arr[], int freq[], int n)
{
    float sum = 0, frequency_sum = 0;
    for (int i = 0; i < n; i++) {
        sum = sum + (float)freq[i] / arr[i];
        frequency_sum  = frequency_sum  + freq[i];
    }
    return frequency_sum / sum;
}
 
// Driver code
int main()
{
    int num[] = { 13, 14, 15, 16, 17 };
    int freq[] = { 2, 5, 13, 7, 3 };
    int n = sizeof(num) / sizeof(num[0]);
    cout << harmonicMean(num, freq, n);
    return 0;
}


Java
// JAVA program to find harmonic mean.
 
class GFG {
     
    // Function that returns harmonic mean.
    static float harmonicMean(int arr[], int freq[],
                              int n)
    {
        float sum = 0, frequency_sum = 0;
        for (int i = 0; i < n; i++) {
            sum = sum + (float)freq[i] / arr[i];
            frequency_sum  = frequency_sum  + freq[i];
        }
        return (frequency_sum / sum);
    }
      
    // Driver code
    public static void main(String args[])
    {
        int num[] = { 13, 14, 15, 16, 17 };
        int freq[] = { 2, 5, 13, 7, 3 };
        int n = num.length;
        System.out.println(harmonicMean(num, freq, n));
    }
}
 
/*This code is contributed by Nikita Tiwari.*/


Python3
# Python 3 program to find harmonic mean.
 
# Function that returns harmonic mean.
def harmonicMean(arr, freq, n) :
    sm = 0
    frequency_sum = 0
    for i in range(0,n) :
        sm = sm + freq[i] / arr[i]
        frequency_sum  = frequency_sum  + freq[i]
     
    return (round(frequency_sum / sm,4))
 
 
# Driver code
num = [ 13, 14, 15, 16, 17 ]
freq = [ 2, 5, 13, 7, 3 ]
n = len(num)
print (harmonicMean(num, freq, n))
 
 
# This code is contributed by Nikita Tiwari.


C#
// C# program to find harmonic mean.
using System;
 
class GFG {
 
    // Function that returns harmonic mean.
    static float harmonicMean(int[] arr, int[] freq,
                                              int n)
    {
        float sum = 0, frequency_sum = 0;
        for (int i = 0; i < n; i++) {
            sum = sum + (float)freq[i] / arr[i];
            frequency_sum = frequency_sum + freq[i];
        }
        return (frequency_sum / sum);
    }
 
    // Driver code
    public static void Main()
    {
        int[] num = { 13, 14, 15, 16, 17 };
        int[] freq = { 2, 5, 13, 7, 3 };
        int n = num.Length;
        Console.WriteLine(harmonicMean(num, freq, n));
    }
}
 
/*This code is contributed by vt_m.*/


PHP


Javascript


Python3
#'harmonic_mean()' new function added in 'Python3.6' onwards.
#Program calculates Harmonic Mean using harmonic_mean()
 
#imports Python statistics library
import statistics
def harmonic_mean():
    list= [13.5, 14.5, 14.8, 15.2, 16.1]
    print(statistics.harmonic_mean(list))
 
#Driver code
harmonic_mean()
 
 
#This code is contributed by 'Abhishek Agrawal'.


输出:

14.7707

如果给定元素及其频率怎么办?
如果给定n个数字,每个数字都有一定的频率,那么我们可以简单地使用公式
谐波平均值=(频率和)/((f 1 / x 1 )+(f 2 / x 2 )+(f 3 / x 3 )+ .. +(f n / x n ))
其中f 1 ,f 2 ,f 3 ,。 。 …,F n的元素的频率,并且x 1,2,X 3,。 。 。,x n是数组的元素。
频率和= f 1 + f 2 + f 3 ,…。 。 。,f n
例子:

Input : num[] = {13, 14, 15, 16, 17}
        freq[] = {2, 5, 13, 7, 3}
Output : 15.0631

C++

// C++ program to find harmonic mean.
#include 
using namespace std;
// Function that returns harmonic mean.
float harmonicMean(int arr[], int freq[], int n)
{
    float sum = 0, frequency_sum = 0;
    for (int i = 0; i < n; i++) {
        sum = sum + (float)freq[i] / arr[i];
        frequency_sum  = frequency_sum  + freq[i];
    }
    return frequency_sum / sum;
}
 
// Driver code
int main()
{
    int num[] = { 13, 14, 15, 16, 17 };
    int freq[] = { 2, 5, 13, 7, 3 };
    int n = sizeof(num) / sizeof(num[0]);
    cout << harmonicMean(num, freq, n);
    return 0;
}

Java

// JAVA program to find harmonic mean.
 
class GFG {
     
    // Function that returns harmonic mean.
    static float harmonicMean(int arr[], int freq[],
                              int n)
    {
        float sum = 0, frequency_sum = 0;
        for (int i = 0; i < n; i++) {
            sum = sum + (float)freq[i] / arr[i];
            frequency_sum  = frequency_sum  + freq[i];
        }
        return (frequency_sum / sum);
    }
      
    // Driver code
    public static void main(String args[])
    {
        int num[] = { 13, 14, 15, 16, 17 };
        int freq[] = { 2, 5, 13, 7, 3 };
        int n = num.length;
        System.out.println(harmonicMean(num, freq, n));
    }
}
 
/*This code is contributed by Nikita Tiwari.*/

Python3

# Python 3 program to find harmonic mean.
 
# Function that returns harmonic mean.
def harmonicMean(arr, freq, n) :
    sm = 0
    frequency_sum = 0
    for i in range(0,n) :
        sm = sm + freq[i] / arr[i]
        frequency_sum  = frequency_sum  + freq[i]
     
    return (round(frequency_sum / sm,4))
 
 
# Driver code
num = [ 13, 14, 15, 16, 17 ]
freq = [ 2, 5, 13, 7, 3 ]
n = len(num)
print (harmonicMean(num, freq, n))
 
 
# This code is contributed by Nikita Tiwari.

C#

// C# program to find harmonic mean.
using System;
 
class GFG {
 
    // Function that returns harmonic mean.
    static float harmonicMean(int[] arr, int[] freq,
                                              int n)
    {
        float sum = 0, frequency_sum = 0;
        for (int i = 0; i < n; i++) {
            sum = sum + (float)freq[i] / arr[i];
            frequency_sum = frequency_sum + freq[i];
        }
        return (frequency_sum / sum);
    }
 
    // Driver code
    public static void Main()
    {
        int[] num = { 13, 14, 15, 16, 17 };
        int[] freq = { 2, 5, 13, 7, 3 };
        int n = num.Length;
        Console.WriteLine(harmonicMean(num, freq, n));
    }
}
 
/*This code is contributed by vt_m.*/

的PHP


Java脚本


输出:

15.0631

在Python使用谐波_mean ()实现数字的谐波均值
一个简单的Python程序,可以使用谐和函数(harmonic_mean())查找谐波平均值。

Python3

#'harmonic_mean()' new function added in 'Python3.6' onwards.
#Program calculates Harmonic Mean using harmonic_mean()
 
#imports Python statistics library
import statistics
def harmonic_mean():
    list= [13.5, 14.5, 14.8, 15.2, 16.1]
    print(statistics.harmonic_mean(list))
 
#Driver code
harmonic_mean()
 
 
#This code is contributed by 'Abhishek Agrawal'.
Output: 14.770680729373778