慕勒3428872
让我们从问题开始:我只是迷路了,似乎不知道该怎么计算简单的移动平均线?鉴于public static class Extensions{ public static IEnumerable<decimal> MovingAvg(this IEnumerable<decimal> source, int period) { var buffer = new Queue<decimal>(); foreach (var value in source) { buffer.Enqueue(value); // sume the buffer for the average at any given time yield return buffer.Sum() / buffer.Count; // Dequeue when needed if (buffer.Count == period) buffer.Dequeue(); } }}用法static void Main(string[] args){ var input = new decimal[] { 1, 2, 2, 4, 5, 6, 6, 6, 9, 10, 11, 12, 13, 14, 15 }; var result = input.MovingAvg(2); Console.WriteLine(string.Join(", ",result));}输出1, 1.5, 2, 3, 4.5, 5.5, 6, 6, 7.5, 9.5, 10.5, 11.5, 12.5, 13.5, 14.5