📜  数组中的平均数

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

数组中的平均数

给定一个正整数序列 a1, a2, ..., an。找到所有这样的索引 i 使得第 i 个元素等于所有其他元素的算术平均值(即除了这个元素之外的所有元素)。
例子 :

Input : 5
        1 2 3 4 5
Output : 1 no. of elements
         2 index of element
Average of 1, 2, 4 & 5 is 3 so the 
output is single index i.e. 3.

Input : 4
        50 50 50 50
Output : 4 no. of elements
         0 1 2 3 index of element
Average of 50, 50, 50 & 50 is 50 and 
all the indexes has the same i.e. 50
so the output is indexes 1, 2, 3 & 4.

C++
// CPP program to print all such indices such
// that the i-th element equals the arithmetic
// mean of all other elements
#include 
using namespace std;
 
// function to find number of elements
// satisfying condition and their indexes
void averageNumbers(int arr[], int n, int sum)
{
 
    int cnt = 0;
 
    // calculating average
    sum /= (double)n;
 
    // counting how many elements
    // satisfies the condition.
    cout << count(arr, arr + n, sum)
         << endl;
    for (int i = 0; i < n; i++) {
        if ((double)arr[i] == sum) {
 
            // output the indices.
            cout << i << " ";
            cnt++;
        }
    }
}
 
// Driver code
int main()
{
    int n;
    int arr[] = { 1, 2, 3, 4, 5 };
    n = sizeof(arr) / sizeof(arr[0]);
    double sum = 0;
    int cnt = 0;
 
    // sum of the elements of the array
    for (int i = 0; i < n; i++) {
        sum += (double)arr[i];
    }
    averageNumbers(arr, n, sum);
    return 0;
}


Java
// Java program to print all such indices such
// that the i-th element equals the arithmetic
// mean of all other elements
public class GFG {
 
// function to find number of elements
// satisfying condition and their indexes
    static void averageNumbers(int arr[], int n, int sum) {
 
        int cnt = 0;
 
        // calculating average
        sum /= (double) n;
 
        // counting how many elements
        // satisfies the condition.
        System.out.println(count(arr, sum));
        for (int i = 0; i < n; i++) {
            if ((double) arr[i] == sum) {
 
                // output the indices.
                System.out.print(i + " ");
                cnt++;
            }
        }
    }
 
    static int count(int[] array, int sum) {
        int count = 0;
        for (int i = 0; i < array.length; i++) {
            if (array[i] == sum) {
                count++;
            }
        }
        return count;
    }
// Driver code
 
    public static void main(String[] args) {
        int n;
        int arr[] = {1, 2, 3, 4, 5};
        n = arr.length;
        int sum = 0;
        int cnt = 0;
 
        // sum of the elements of the array
        for (int i = 0; i < n; i++) {
            sum += (double) arr[i];
        }
        averageNumbers(arr, n, sum);
 
    }
 
}
// This code is contributed by 29AjayKumar


Python3
# Python 3 program to print all such indices
# such that the i-th element equals the
# arithmetic mean of all other elements
 
# Function to find number of elements
# satisfying condition and their indexes
def averageNumbers(arr, n, sum):
    cnt = 0
 
    # calculating average
    sum /= n
 
    # counting how many elements
    # satisfies the condition.
    print(count(arr, sum))
    for i in range(0, n):
        if (arr[i] == sum):
             
            # output the indices.
            print(i, " ")
            cnt += 1
             
def count(array, sum):
    count = 0
    for i in range(0, len(array)):
        if (array[i] == sum):
            count += 1
    return count
         
# Driver code
if __name__ == '__main__':
    n = 0
    arr = [ 1, 2, 3, 4, 5 ]
    n = len(arr)
    sum = 0
    cnt = 0
 
    # sum of the elements of the array
    for i in range(0, n):
        sum += arr[i]
    averageNumbers(arr, n, sum)
 
# This code contributed by 29AjayKumar


C#
// C# program to print all such indices such
// that the i-th element equals the arithmetic
// mean of all other elements
using System;
public class GFG {
  
// function to find number of elements
// satisfying condition and their indexes
    static void averageNumbers(int []arr, int n, int sum) {
  
        int cnt = 0;
  
        // calculating average
        sum /=  n;
  
        // counting how many elements
        // satisfies the condition.
        Console.WriteLine(count(arr, sum));
        for (int i = 0; i < n; i++) {
            if ((double) arr[i] == sum) {
  
                // output the indices.
                Console.Write(i + " ");
                cnt++;
            }
        }
    }
  
    static int count(int[] array, int sum) {
        int count = 0;
        for (int i = 0; i < array.Length; i++) {
            if (array[i] == sum) {
                count++;
            }
        }
        return count;
    }
// Driver code
  
    public static void Main() {
        int n;
        int []arr = {1, 2, 3, 4, 5};
        n = arr.Length;
        int sum = 0;
  
        // sum of the elements of the array
        for (int i = 0; i < n; i++) {
            sum += arr[i];
        }
        averageNumbers(arr, n, sum);
  
    }
  
}
// This code is contributed by 29AjayKumar


PHP


Javascript


输出:

1
2