如2,5,8,6,5,12,8,7,9,3,7,1,15,6  去掉最小数,1,2 去掉最大数,12,15 再求剩下的平均值,能用一个函数比较快速地算出么?

解决方案 »

  1.   


            static void Main(string[] args)
            {
                Console.WriteLine(GetAverage(new int[] { 2, 5, 8, 6, 5, 12, 8, 7, 9, 3, 7, 1, 15, 6 }));
            }        static double GetAverage(int[] array)
            {
                if (array.Length > 5)
                {
                    Array.Sort(array);
                    int total = 0;
                    for (int i = 2; i <= array.Length - 3; i++)
                        total += array[i];
                    return (double)total / (array.Length - 4);
                }
                return 0;
            }
    /*
    输出6.4
    */
      

  2.   

    不是,这组数据是在一个object的items里,比如object.items[i].x=10
      

  3.   

    添加到Arrylist里边在sort  一样
      

  4.   

                List<int> list = new List<int> { 2, 5, 8, 6, 5, 12, 8, 7, 9, 3, 7, 1, 15, 6 };
                list.Sort();
                list.RemoveRange(list.Count - 2, 2);
                list.RemoveRange(0, 2);
                double avg = list.Average();