• Console算法[]数组求最大值和最小值(只能遍历一次)


    ylbtech-Arithmetic:Console-算法[]-数组求最大值和最小值(只能遍历一次)
     
    1.A,Demo(案例)

     Console-算法[]-数组求最大值和最小值(只能遍历一次)

    1.B,Solution(解决方案)
    1.B.1, 方法一
    using System;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                int[] arrayList = new int[] {2,4,1,6,3,6,9,8 };
                int min = 0;
                int max = 9;
    
                foreach (int i in arrayList)
                {
                    if (i > min)
                    {
                        max = i;
                    }
                    if (i < max)
                    {
                        min = i;
                    }
                }
    
                Console.WriteLine("max={0}",max);
                Console.WriteLine("min={0}",min);
            }       
        }   
    }
    View Code

    1.B.2, 方法二

    using System;
    namespace ConsoleApplication1
    {
        class Programe
        {
            /// <summary>
            /// ylb: 算法
            /// </summary>
            /// <param name="args"></param>
            static void Main(string[] args)
            {
                int max = 0;    //假设最大值是 0
                int min = 9;    //假设最小值是 9
                int[] arrayList = new int[] { 3, 5, 7, 1, 8 };
    
                //循环一次
                foreach (int i in arrayList)
                {
                    //如果 max 小于 i 则,把 i 的值赋予 max
                    if (max < i)
                        max = i;
                    //如果 min 大于 i 则,把 i 的值赋予 min
                    if (min > i)
                        min = i;
                }
    
                Console.WriteLine("min={0}", min);
                Console.WriteLine("max={0}", max);
            }
        }
    }
    View Code
    1.B.2, 方法三(最佳)
    using System;
    
    namespace ConsoleAppliction1
    {
        class Program
        {
            /// <summary>
            /// ylb:算法
            /// 15:52 2014-03-31
            /// </summary>
            /// <param name="args"></param>
            static void Main(string[] args)
            {
                int[] array = new int[] { 2, 4, 1, 6, 3, 6, 9, 8 };
                int max = array[0];
                int min = array[0];
    
                for (int i = 1; i < array.Length; i++)
                {
                    //如果 min 大于 array[i] 则,把 array[i] 的值赋予 min
                    if (min > array[i])
                    {
                        min = array[i];
                    }
                    //如果 max 小于 array[i] 则,把 array[i] 的值赋予 max
                    if (max < array[i])
                    {
                        max = array[i];
                    }
                }
    
                Console.WriteLine("max=" + max);
                Console.WriteLine("min=" + min);
            }
        }
    }
    View Code
    1.C,Execution Result(运行结果)
     
    warn 作者:ylbtech
    出处:http://ylbtech.cnblogs.com/
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    python爬虫,scrapy,获取响应的cookie,获取返回的cookie
    this指向
    闭包的10种形式
    nodejs 公私钥文件加密解密
    mysql基础知识
    nodejs 连接mysql 集群,开启事务,事务回滚封装
    pm2 启动eggjs,
    js身份证验证,二代身份证,大陆,权重验证,正规
    nodejs限制IP一段时间内的访问次数
    nodejs链接mysql集群,nodejs PoolCluster : Error: Too many connections
  • 原文地址:https://www.cnblogs.com/ylbtech/p/3113499.html
Copyright © 2020-2023  润新知