📜  使用递归的数组平均值

📅  最后修改于: 2022-05-13 01:57:51.576000             🧑  作者: Mango

使用递归的数组平均值

求数组元素的均值。

Mean = (Sum of elements of the Array) /
       (Total no of elements in Array)

例子:

Input : 1 2 3 4 5
Output : 3

Input : 1 2 3
Output : 2

要使用递归找到平均值,假设问题已经解决了 N-1 即你必须找到 n

Sum of first N-1 elements = 
                 (Mean of N-1 elements)*(N-1)

Mean of N elements = (Sum of first N-1 elements + 
                      N-th elements) / (N)

注意:由于数组索引从 0 开始,我们使用 A[N-1] 访问第 N 个元素。

C++
// Recursive C++ program to find mean of array
#include 
using namespace std;
 
// Function Definition of findMean function
float findMean(int A[], int N)
{
    if (N == 1)
        return (float)A[N-1];
    else
        return ((float)(findMean(A, N-1)*(N-1) +
                                    A[N-1]) / N);
}
 
// Main Calling Function
int main()
{
    float Mean = 0;
    int A[] = {1, 2, 3, 4, 5};
    int N = sizeof(A)/sizeof(A[0]);
    cout << " "<< findMean(A, N);
    return 0;
}
 
// this code is contributed by shivanisinghss2110


C
// Recursive C program to find mean of array
#include
 
// Function Definition of findMean function
float findMean(int A[], int N)
{
    if (N == 1)
        return (float)A[N-1];
    else
        return ((float)(findMean(A, N-1)*(N-1) +
                                    A[N-1]) / N);
}
 
// Main Calling Function
int main()
{
    float Mean = 0;
    int A[] = {1, 2, 3, 4, 5};
    int N = sizeof(A)/sizeof(A[0]);
    printf("%.2f\n", findMean(A, N));
    return 0;
}


Java
// Recursive Java program to find mean of array
class CalcMean
{
    // Function Definition of findMean function
    static float findMean(int A[], int N)
    {
        if (N == 1)
            return (float)A[N-1];
        else
            return ((float)(findMean(A, N-1)*(N-1) +
                                        A[N-1]) / N);
    }
     
    // main Function
    public static void main (String[] args)
    {
        float Mean = 0;
        int A[] = {1, 2, 3, 4, 5};
        int N = A.length;
        System.out.println(findMean(A, N));
    }
}


Python3
# Recursive Python3 program to
# find mean of array
 
# Function Definition of findMean function
def findMean(A, N):
 
    if (N == 1):
        return A[N - 1]
    else:
        return ((findMean(A, N - 1) *
                (N - 1) + A[N - 1]) / N)
 
# Driver Code
Mean = 0
A = [1, 2, 3, 4, 5]
N = len(A)
print(findMean(A, N))
 
# This code is contributed by Anant Agarwal.


C#
// Recursive C# program to find mean of array
using System;
 
class CalcMean
{
    // Function Definition of findMean function
    static float findMean(int []A, int N)
    {
        if (N == 1)
            return (float)A[N - 1];
        else
            return ((float)(findMean(A, N - 1) *
                           (N - 1) + A[N - 1]) / N);
    }
      
    // Driver code
    public static void Main()
    {
        //float Mean = 0;
        int []A = {1, 2, 3, 4, 5};
        int N = A.Length;
        Console.WriteLine(findMean(A, N));
    }
}
 
// This code is contributed by Anant Agarwal.


PHP


Javascript


输出:

3