zoukankan      html  css  js  c++  java
  • 数组求最值和平均数的算法

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




    作者:艾孜尔江

  • 相关阅读:
    分布式与云计算有什么区别?
    多线程学习笔记
    docker解决报错WARNING: IPv4 forwarding is disabled. Networking will not work.
    Docker问题解决:Error response from daemon: Get https://registry-1.docker.io/v2/: dial tcp: lookup registry-1.docker.io: no such host
    docker学习笔记
    linux命令学习
    Xftp5提示要继续使用此程序,您必须应用最新的更新的解决方案
    Springboot学习笔记
    maven学习笔记
    Spring学习笔记(6)
  • 原文地址:https://www.cnblogs.com/ezhar/p/13881236.html
Copyright © 2011-2022 走看看