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


    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;
            }
        }
    }
    




    作者:艾孜尔江

  • 相关阅读:
    centos 7.3 配置vnc 服务 图形界面登录
    安装vnc出现的问题
    HDFS文件系统的JAVA-API操作(一)
    kafka offset的存储问题
    kafka查询topic属性含义
    java静态类、静态方法、静态代码块,静态变量及实例方法,实例变量初始化顺序及内存管理,机制
    avg函数忽略null值
    为服务开启BBR加速
    正则表达式转义所有特殊符号
    Scala 运算符优先顺序
  • 原文地址:https://www.cnblogs.com/ezhar/p/13881236.html
Copyright © 2020-2023  润新知