📜  自然数加权均值的程序。

📅  最后修改于: 2021-04-24 17:18:22             🧑  作者: Mango

给定一个自然数数组,另一个具有相应数字权重的数组。然后,我们必须计算加权平均值。

其中x(bar)称为加权平均值,x [i]是数组的元素,W [i]是数组x [i]的元素的权重。

例子 –

Input : 
X[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
W[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} 
weighted mean 
= (W[0] * X[0] + W[1] * X[1] + W[2] * X[2] + . . . +
   W[n-1] * X[n-1]) / (W[0] + W[1] + W[2] + . . . + W[n-1])
= (1 * 1 + 2 * 2 + 3 * 3 + . . . + 10 * 10) /
  (1 + 2 + 3 + . . . + 10)
= 385 / 55 = 7
Output : 7

Input : 
X[] = {3, 4, 5, 6, 7}
W[] = {4, 5, 6, 7, 8}
weighted mean 
= (W[0] * X[0] + W[1] * X[1] + W[2] * X[2] + . . . +
   W[n-1] * X[n-1]) / (W[0] + W[1] + W[2] + . . . + W[n-1])
= (3 * 4 + 4 * 5 + 5 * 6 + 6 * 7 + 7 * 8) / 
  (4 + 5 + 6 + 7 + 8)
= 160 / 30 = 5.33333
Output : 5.33333

一个解决加权均值的简单解决方案。

C++
// Program to find weighted mean of
// natural numbers.
#include
using namespace std;
  
// Function to calculate weighted mean.
float weightedMean(int X[], int W[], int n)
{
    int sum = 0, numWeight = 0;
  
    for (int i = 0; i < n; i++)
    {
        numWeight = numWeight + X[i] * W[i];
        sum = sum + W[i];
    }
  
    return (float)numWeight / sum;
}
  
// Driver program to test the function.
int main()
{
    // Take num array and corresponding weight  
    // array and initialize it.
    int X[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int W[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  
    // Calculate the size of array.
    int n = sizeof(X)/sizeof(X[0]);
    int m = sizeof(W)/sizeof(W[0]);
  
    // Check the size of both array is equal or not.
    if (n == m)
        cout << weightedMean(X, W, n);
    else
        cout << "-1";
    return 0;
}


Java
// JAVA Program to find weighted mean
// of natural numbers.
  
class GFG {
      
    // Function to calculate weighted mean.
    static float weightedMean(int X[], int W[],
                                        int n)
    {
        int sum = 0, numWeight = 0;
       
        for (int i = 0; i < n; i++)
        {
            numWeight = numWeight + X[i] * W[i];
            sum = sum + W[i];
        }
       
        return (float)(numWeight) / sum;
    }
       
    // Driver program to test the function.
    public static void main(String args[])
    {
        // Take num array and corresponding 
        // weight array and initialize it.
        int X[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        int W[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
       
        // Calculate the size of array.
        int n = X.length;
        int m = W.length;
       
        // Check the size of both array is 
        // equal or not.
        if (n == m)
            System.out.println(weightedMean(X, W, n));
        else
            System.out.println("-1" );
          
    } 
}
  
/*This code is contributed by Nikita Tiwari.*/


Python
# Python Program to find weighted mean of
# natural numbers.
  
# Function to calculate weighted mean.
def weightedMean(X,W,n) :
    sum = 0
    numWeight = 0
    i = 0
    while  i < n :
          
        numWeight = numWeight + X[i] * W[i]
        sum = sum + W[i]
        i = i + 1
   
    return (float)(numWeight / sum)
  
   
# Driver program to test the function.
  
# Take num array and corresponding weight  
# array and initialize it.
X = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
W = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  
# Calculate the size of array.
n = len(X)
m = len(W)
   
# Check the size of both array is equal or not.
if (n == m) :
    print weightedMean(X, W, n)
else :
    print "-1"
      
# This coe is contributed by Nikita Tiwari.


C#
// C# Program to find weighted mean
// of natural numbers.
using System;
  
class GFG {
      
    // Function to calculate weighted mean.
    static float weightedMean(int []X, int []W,
                                        int n)
    {
        int sum = 0, numWeight = 0;
      
        for (int i = 0; i < n; i++)
        {
            numWeight = numWeight + X[i] * W[i];
            sum = sum + W[i];
        }
      
        return (float)(numWeight) / sum;
    }
      
    // Driver program to test the function.
    public static void Main()
    {
          
        // Take num array and corresponding 
        // weight array and initialize it.
        int []X = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        int []W = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
      
        // Calculate the size of array.
        int n = X.Length;
        int m = W.Length;
      
        // Check the size of both array is 
        // equal or not.
        if (n == m)
            Console.WriteLine(weightedMean(X, W, n));
        else
            Console.WriteLine("-1" );
    } 
}
  
// This code is contributed by vt_m.


PHP


C++
// Program to find weighted mean of first
// n natural numbers using formula.
#include
using namespace std;
  
// Returns weighted mean assuming for numbers
// {1, 2, ..n} and weights {1, 2, .. n}
int weightedMean(int n)
{
    return (2 * n + 1)/3;
}
  
// Driver program to test the function.
int main()
{
    int n = 10;
    cout << weightedMean(n);
    return 0;
}


Java
// Program to find weighted mean of first
// n natural numbers using formula.
import java.io.*;
  
public class GFG {
      
    // Returns weighted mean assuming for numbers
    // {1, 2, ..n} and weights {1, 2, .. n}
    static int weightedMean(int n)
    {
        return (2 * n + 1)/3;
    }
      
    // Driver program to test the function.
  
    static public void main (String[] args)
    {
        int n = 10;
          
        System.out.println(weightedMean(n));
      
    }
}
  
// This code is contributed by vt_m.


Python 3
# Program to find weighted mean of first
# n natural numbers using formula.
  
# Returns weighted mean assuming for numbers
# 1, 2, ..n and weights 1, 2, .. n
def weightedMean(n):
  
    return (2 * n + 1) / 3
  
# Driver program to test the function.
n = 10
print(int(weightedMean(n)))
# This code is contributed by smita


C#
// Program to find weighted mean of first
// n natural numbers using formula.
using System;
  
public class GFG {
      
    // Returns weighted mean assuming for numbers
    // {1, 2, ..n} and weights {1, 2, .. n}
    static int weightedMean(int n)
    {
        return (2 * n + 1) / 3;
    }
      
    // Driver program to test the function.
  
    static public void Main ()
    {
        int n = 10;
          
        Console.WriteLine(weightedMean(n));
    }
}
  
// This code is contributed by vt_m.


PHP


输出 –

7

第二种方法–计算前n个自然数的加权平均值。它是用于计算前n个自然数的加权平均值的公式。在这种方法中,我们首先给出了n个自然数,它们的权重也是自然数。然后我们生成公式

Weighted Mean 
= (W[0] * X[0] + W[1] * X[1] + W[2] * X[2] + . . . +
      W[n-1] * X[n-1]) / (W[0] + W[1] + W[2] + . . . + W[n-1])
= (1 * 1 + 2 * 2 + 3 * 3 + . . . + n * n) / (1 + 2 + 3 + . . . + n)
= (n * (n + 1) * (2 * n + 1) / 6) / (n * (n + 1) / 2)

Weighted Mean = (2 * n + 1) / 3
 
Example: Weighted mean of first 10 natural numbers 
n = 10
Weighted mean 
= (2 * 10 + 1) / 3 = 21 / 3 = 7

C++

// Program to find weighted mean of first
// n natural numbers using formula.
#include
using namespace std;
  
// Returns weighted mean assuming for numbers
// {1, 2, ..n} and weights {1, 2, .. n}
int weightedMean(int n)
{
    return (2 * n + 1)/3;
}
  
// Driver program to test the function.
int main()
{
    int n = 10;
    cout << weightedMean(n);
    return 0;
}

Java

// Program to find weighted mean of first
// n natural numbers using formula.
import java.io.*;
  
public class GFG {
      
    // Returns weighted mean assuming for numbers
    // {1, 2, ..n} and weights {1, 2, .. n}
    static int weightedMean(int n)
    {
        return (2 * n + 1)/3;
    }
      
    // Driver program to test the function.
  
    static public void main (String[] args)
    {
        int n = 10;
          
        System.out.println(weightedMean(n));
      
    }
}
  
// This code is contributed by vt_m.

的Python 3

# Program to find weighted mean of first
# n natural numbers using formula.
  
# Returns weighted mean assuming for numbers
# 1, 2, ..n and weights 1, 2, .. n
def weightedMean(n):
  
    return (2 * n + 1) / 3
  
# Driver program to test the function.
n = 10
print(int(weightedMean(n)))
# This code is contributed by smita

C#

// Program to find weighted mean of first
// n natural numbers using formula.
using System;
  
public class GFG {
      
    // Returns weighted mean assuming for numbers
    // {1, 2, ..n} and weights {1, 2, .. n}
    static int weightedMean(int n)
    {
        return (2 * n + 1) / 3;
    }
      
    // Driver program to test the function.
  
    static public void Main ()
    {
        int n = 10;
          
        Console.WriteLine(weightedMean(n));
    }
}
  
// This code is contributed by vt_m.

的PHP


输出 –

7