寻找简单移动平均线的程序 |第 2 组
简单移动平均线是从某个 t 时间段的数据中获得的平均值。在正常平均值中,它的值会随着数据的变化而变化,但在这种类型的平均值中,它也会随着时间间隔而变化。我们得到某个时期 t 的平均值,然后我们删除一些以前的数据。我们再次得到新的平均值,并且这个过程继续进行。这就是为什么它是移动平均线。这在金融市场上有很大的应用。或者,这可以简单地可视化如下。
给定一个大小为N的arr[] ,仅包含正整数和一个整数K 。任务是计算前K个元素的简单移动平均值。
例子:
Input: { 1, 3, 5, 6, 8 }, K = 3
Output: 0.33 1.33 3.00 4.67 6.33
Explanation: New number added is 1.0, SMA = 0.33
New number added is 3.0, SMA = 1.33
New number added is 5.0, SMA = 3.0
New number added is 6.0, SMA = 4.67
New number added is 8.0, SMA = 6.33
Input: Array[]= {2, 5, 7, 3, 11, 9, 13, 12}, K = 2
Output: 1.0 3.5 6 5 7 10 11 12.5
天真的方法:这使用两个嵌套循环。外循环从左到右遍历数组。内循环计算每个索引的K个先前元素的平均值,包括它自己。最后,打印移动平均值。外循环从索引 K 本身开始遍历。我们可以直接显示输出,而不是存储结果,以避免使用额外的空格。
时间复杂度: O(N*K)
空间复杂度: O(1)
高效方法:本问题的Set-1中讨论了有效方法。
空间优化方法:这使用滑动窗口来提高时间效率和空间优化。大小为K的窗口从索引 K 开始,然后为每个索引打印移动平均值。
下面是上述方法的实现。
C++
// C++ code to find the simple moving average
#include
#include
using namespace std;
// Function to compute moving average
// of previous K elements
void ComputeMovingAverage(int arr[], int N,
int K)
{
int i;
float sum = 0;
// Initial sum of K elements.
for (i = 0; i < K; i++) {
sum += arr[i];
cout << setprecision(2) << std::fixed;
cout << sum / K << " ";
}
// Compute MA from index K
float avg;
for (i = K; i < N; i++) {
sum -= arr[i - K];
sum += arr[i];
avg = sum / K;
cout << setprecision(2) << std::fixed;
cout << avg << " ";
}
}
// Driver code
int main()
{
int arr[] = { 1, 3, 5, 6, 8 };
int N = sizeof(arr) / sizeof(arr[0]);
int K = 3;
ComputeMovingAverage(arr, N, K);
return 0;
}
Java
// Java code to find the simple moving average
import java.util.*;
class GFG{
// Function to compute moving average
// of previous K elements
static void ComputeMovingAverage(int arr[], int N,
int K)
{
int i;
float sum = 0;
// Initial sum of K elements.
for (i = 0; i < K; i++) {
sum += arr[i];
System.out.printf("%.2f ",sum / K);
}
// Compute MA from index K
for (i = K; i < N; i++) {
sum -= arr[i - K];
sum += arr[i];
System.out.printf("%.2f ",sum / K);
}
}
// Driver code
public static void main(String[] args)
{
int arr[] = { 1, 3, 5, 6, 8 };
int N = arr.length;
int K = 3;
ComputeMovingAverage(arr, N, K);
}
}
// This code is contributed by Rajput-Ji
Python3
# Python code for the above approach
# Function to compute moving average
# of previous K elements
def ComputeMovingAverage(arr, N, K):
i = None
sum = 0
# Initial sum of K elements.
for i in range(K):
sum += arr[i]
print("%.2f"%(sum / K), end= " ")
# Compute MA from index K
for i in range(K, N):
sum -= arr[i - K]
sum += arr[i]
avg = sum / K
print("%.2f"%(avg), end =" ")
# Driver code
arr = [1, 3, 5, 6, 8]
N = len(arr)
K = 3
ComputeMovingAverage(arr, N, K)
# This code is contributed by Saurabh Jaiswal
C#
// C# code to find the simple moving average
using System;
class GFG {
// Function to compute moving average
// of previous K elements
static void ComputeMovingAverage(int[] arr, int N,
int K)
{
int i;
float sum = 0;
// Initial sum of K elements.
for (i = 0; i < K; i++) {
sum += arr[i];
Console.Write(Math.Round((sum / K),2) + " ");
}
// Compute MA from index K
for (i = K; i < N; i++) {
sum -= arr[i - K];
sum += arr[i];
Console.Write(Math.Round(sum / K, 2) + " ");
}
}
// Driver code
public static void Main(string[] args)
{
int[] arr = { 1, 3, 5, 6, 8 };
int N = arr.Length;
int K = 3;
ComputeMovingAverage(arr, N, K);
}
}
// This code is contributed by ukasp.
Javascript
0.33 1.33 3.00 4.67 6.33
时间复杂度: O(N)
空间复杂度: O(1)