难度等级:新秀
给定数字流,请在每个点上打印该流的平均值(或均值)。例如,让我们将流视为10、20、30、40、50、60,…
Average of 1 numbers is 10.00
Average of 2 numbers is 15.00
Average of 3 numbers is 20.00
Average of 4 numbers is 25.00
Average of 5 numbers is 30.00
Average of 6 numbers is 35.00
..................
要打印流的均值,我们需要找出如何在向流中添加新数字时求平均值。为此,我们需要的是对流中到目前为止看到的数字,先前的平均值和新数字进行计数。令n为计数, prev_avg为先前的平均值,x为要添加的新数字。包括x个数之后的平均值可以写为(prev_avg * n + x)/(n + 1) 。
C++
#include
// Returns the new average after including x
float getAvg(float prev_avg, int x, int n)
{
return (prev_avg * n + x) / (n + 1);
}
// Prints average of a stream of numbers
void streamAvg(float arr[], int n)
{
float avg = 0;
for (int i = 0; i < n; i++) {
avg = getAvg(avg, arr[i], i);
printf("Average of %d numbers is %f \n", i + 1, avg);
}
return;
}
// Driver program to test above functions
int main()
{
float arr[] = { 10, 20, 30, 40, 50, 60 };
int n = sizeof(arr) / sizeof(arr[0]);
streamAvg(arr, n);
return 0;
}
Java
// Java program to find average
// of a stream of numbers
class GFG {
// Returns the new average after including x
static float getAvg(float prev_avg, float x, int n)
{
return (prev_avg * n + x) / (n + 1);
}
// Prints average of a stream of numbers
static void streamAvg(float arr[], int n)
{
float avg = 0;
for (int i = 0; i < n; i++)
{
avg = getAvg(avg, arr[i], i);
System.out.printf("Average of %d numbers is %f \n",
i + 1, avg);
}
return;
}
// Driver program to test above functions
public static void main(String[] args)
{
float arr[] = { 10, 20, 30, 40, 50, 60 };
int n = arr.length;
streamAvg(arr, n);
}
}
// This code is contributed by Smitha Dinesh Semwal
Python3
# Returns the new average
# after including x
def getAvg(prev_avg, x, n):
return ((prev_avg *
n + x) /
(n + 1));
# Prints average of
# a stream of numbers
def streamAvg(arr, n):
avg = 0;
for i in range(n):
avg = getAvg(avg, arr[i], i);
print("Average of ", i + 1,
" numbers is ", avg);
# Driver Code
arr = [10, 20, 30,
40, 50, 60];
n = len(arr);
streamAvg(arr, n);
# This code is contributed
# by mits
C#
// C# program to find average
// of a stream of numbers
using System;
class GFG
{
// Returns the new average
// after including x
static float getAvg(float prev_avg,
float x, int n)
{
return (prev_avg * n + x) / (n + 1);
}
// Prints average of
// a stream of numbers
static void streamAvg(float[] arr,
int n)
{
float avg = 0;
for (int i = 0; i < n; i++)
{
avg = getAvg(avg, arr[i], i);
Console.WriteLine("Average of {0} " +
"numbers is {1}",
i + 1, avg);
}
return;
}
// Driver Code
public static void Main(String[] args)
{
float[] arr = {10, 20, 30,
40, 50, 60};
int n = arr.Length;
streamAvg(arr, n);
}
}
// This code is contributed by mits
PHP
Javascript
C++
#include
using namespace std;
// Returns the new average after including x
float getAvg(int x)
{
static int sum, n;
sum += x;
return (((float)sum) / ++n);
}
// Prints average of a stream of numbers
void streamAvg(float arr[], int n)
{
float avg = 0;
for (int i = 0; i < n; i++)
{
avg = getAvg(arr[i]);
cout<<"Average of "<
C
#include
// Returns the new average after including x
float getAvg(int x)
{
static int sum, n;
sum += x;
return (((float)sum) / ++n);
}
// Prints average of a stream of numbers
void streamAvg(float arr[], int n)
{
float avg = 0;
for (int i = 0; i < n; i++) {
avg = getAvg(arr[i]);
printf("Average of %d numbers is %f \n", i + 1, avg);
}
return;
}
// Driver program to test above functions
int main()
{
float arr[] = { 10, 20, 30, 40, 50, 60 };
int n = sizeof(arr) / sizeof(arr[0]);
streamAvg(arr, n);
return 0;
}
Java
// Java program to return
// Average of a stream of numbers
class GFG
{
static int sum, n;
// Returns the new average
// after including x
static float getAvg(int x)
{
sum += x;
return (((float)sum) / ++n);
}
// Prints average of a
// stream of numbers
static void streamAvg(float[] arr,
int n)
{
float avg = 0;
for (int i = 0; i < n; i++)
{
avg = getAvg((int)arr[i]);
System.out.println("Average of "+ (i + 1) +
" numbers is " + avg);
}
return;
}
// Driver Code
public static void main(String[] args)
{
float[] arr = new float[]{ 10, 20, 30,
40, 50, 60 };
int n = arr.length;
streamAvg(arr, n);
}
}
// This code is contributed by mits
Python3
# Returns the new average
# after including x
def getAvg(x, n, sum):
sum = sum + x;
return float(sum) / n;
# Prints average of a
# stream of numbers
def streamAvg(arr, n):
avg = 0;
sum = 0;
for i in range(n):
avg = getAvg(arr[i], i + 1, sum);
sum = avg * (i + 1);
print("Average of ", end = "");
print(i + 1, end = "");
print(" numbers is ", end = "");
print(avg);
return;
# Driver Code
arr= [ 10, 20, 30,
40, 50, 60 ];
n = len(arr);
streamAvg(arr,n);
# This code is contributed by mits
C#
using System;
class GFG
{
static int sum, n;
// Returns the new average
// after including x
static float getAvg(int x)
{
sum += x;
return (((float)sum) / ++n);
}
// Prints average of a
// stream of numbers
static void streamAvg(float[] arr, int n)
{
float avg = 0;
for (int i = 0; i < n; i++)
{
avg = getAvg((int)arr[i]);
Console.WriteLine("Average of {0} numbers " +
"is {1}", (i + 1), avg);
}
return;
}
// Driver Code
static int Main()
{
float[] arr = new float[]{ 10, 20, 30,
40, 50, 60 };
int n = arr.Length;
streamAvg(arr, n);
return 0;
}
}
// This code is contributed by mits
PHP
Javascript
输出 :
Average of 1 numbers is 10.000000
Average of 2 numbers is 15.000000
Average of 3 numbers is 20.000000
Average of 4 numbers is 25.000000
Average of 5 numbers is 30.000000
Average of 6 numbers is 35.000000
可以使用以下更改来优化上述函数getAvg()。我们可以通过使用静态变量来避免使用prev_avg和元素数量(假设仅调用此函数来获取流的平均值)。以下是经过优化的版本。
C++
#include
using namespace std;
// Returns the new average after including x
float getAvg(int x)
{
static int sum, n;
sum += x;
return (((float)sum) / ++n);
}
// Prints average of a stream of numbers
void streamAvg(float arr[], int n)
{
float avg = 0;
for (int i = 0; i < n; i++)
{
avg = getAvg(arr[i]);
cout<<"Average of "<
C
#include
// Returns the new average after including x
float getAvg(int x)
{
static int sum, n;
sum += x;
return (((float)sum) / ++n);
}
// Prints average of a stream of numbers
void streamAvg(float arr[], int n)
{
float avg = 0;
for (int i = 0; i < n; i++) {
avg = getAvg(arr[i]);
printf("Average of %d numbers is %f \n", i + 1, avg);
}
return;
}
// Driver program to test above functions
int main()
{
float arr[] = { 10, 20, 30, 40, 50, 60 };
int n = sizeof(arr) / sizeof(arr[0]);
streamAvg(arr, n);
return 0;
}
Java
// Java program to return
// Average of a stream of numbers
class GFG
{
static int sum, n;
// Returns the new average
// after including x
static float getAvg(int x)
{
sum += x;
return (((float)sum) / ++n);
}
// Prints average of a
// stream of numbers
static void streamAvg(float[] arr,
int n)
{
float avg = 0;
for (int i = 0; i < n; i++)
{
avg = getAvg((int)arr[i]);
System.out.println("Average of "+ (i + 1) +
" numbers is " + avg);
}
return;
}
// Driver Code
public static void main(String[] args)
{
float[] arr = new float[]{ 10, 20, 30,
40, 50, 60 };
int n = arr.length;
streamAvg(arr, n);
}
}
// This code is contributed by mits
Python3
# Returns the new average
# after including x
def getAvg(x, n, sum):
sum = sum + x;
return float(sum) / n;
# Prints average of a
# stream of numbers
def streamAvg(arr, n):
avg = 0;
sum = 0;
for i in range(n):
avg = getAvg(arr[i], i + 1, sum);
sum = avg * (i + 1);
print("Average of ", end = "");
print(i + 1, end = "");
print(" numbers is ", end = "");
print(avg);
return;
# Driver Code
arr= [ 10, 20, 30,
40, 50, 60 ];
n = len(arr);
streamAvg(arr,n);
# This code is contributed by mits
C#
using System;
class GFG
{
static int sum, n;
// Returns the new average
// after including x
static float getAvg(int x)
{
sum += x;
return (((float)sum) / ++n);
}
// Prints average of a
// stream of numbers
static void streamAvg(float[] arr, int n)
{
float avg = 0;
for (int i = 0; i < n; i++)
{
avg = getAvg((int)arr[i]);
Console.WriteLine("Average of {0} numbers " +
"is {1}", (i + 1), avg);
}
return;
}
// Driver Code
static int Main()
{
float[] arr = new float[]{ 10, 20, 30,
40, 50, 60 };
int n = arr.Length;
streamAvg(arr, n);
return 0;
}
}
// This code is contributed by mits
的PHP
Java脚本
输出:
Average of 1 numbers is 10.0
Average of 2 numbers is 15.0
Average of 3 numbers is 20.0
Average of 4 numbers is 25.0
Average of 5 numbers is 30.0
Average of 6 numbers is 35.0