数组求最值和平均数的算法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ArrayMinMaxAveragge
{
    class Program
    {
        static void Main(string[] args)
        {

            int[] array = new int[] { 1, 2, 4, 3, 0, -1, 34, 545, 2, 34, 53, 24, 97, 48, 31, 977 };

            Console.WriteLine("--------------Array自身方法-----------------");
            Console.WriteLine("Min:{0}", array.Min());
            Console.WriteLine("Max:{0}", array.Max());
            Console.WriteLine("Average:{0}", array.Average());

            Console.WriteLine("---------------内部实现方法------------------");
            int min = Program.Min(array);
            int max = Program.Max(array);
            double? average = Program.Average(array);
            Console.WriteLine("Min:" + min);
            Console.WriteLine("Max:" + max);
            Console.WriteLine("Average:" + average);
            Console.Read();
        }

        /// <summary>
        /// 最小值
        /// </summary>
        /// <param name="array"></param>
        /// <returns></returns>
        public static int Min(int[] array)
        {
            if (array == null) throw new Exception("数组空异常");
            int value = 0;
            bool hasValue = false;
            foreach (int x in array)
            {
                if (hasValue)
                {
                    if (x < value) value = x;
                }
                else
                {
                    value = x;
                    hasValue = true;
                }
            }
            if (hasValue) return value;
            throw new Exception("没找到");
        }

        /// <summary>
        /// 最大值
        /// </summary>
        /// <param name="array"></param>
        /// <returns></returns>
        public static int Max(int[] array)
        {
            if (array == null) throw new Exception("数组空异常");
            int value = 0;
            bool hasValue = false;
            foreach (int x in array)
            {
                if (hasValue)
                {
                    if (x > value)
                        value = x;
                }
                else
                {
                    value = x;
                    hasValue = true;
                }
            }
            if (hasValue) return value;
            throw new Exception("没找到");
        }

        /// <summary>
        /// 平均值
        /// </summary>
        /// <param name="source"></param>
        /// <returns></returns>
        public static double? Average(int[] array)
        {
            if (array == null) throw new Exception("数组空异常");
            long sum = 0;
            long count = 0;
            checked
            {
                foreach (int? v in array)
                {
                    if (v != null)
                    {
                        int a = v.GetValueOrDefault();
                        sum += v.GetValueOrDefault();
                        count++;
                    }
                }
            }
            if (count > 0) return (double)sum / count;
            return null;
        }
    }
}




作者:艾孜尔江

posted @ 2020-10-26 20:59  艾孜尔江  阅读(187)  评论(0编辑  收藏  举报