Console-算法[]-数组求最大值和最小值(只能遍历一次)
生活随笔
收集整理的這篇文章主要介紹了
Console-算法[]-数组求最大值和最小值(只能遍历一次)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
| ylbtech-Arithmetic:Console-算法[]-數(shù)組求最大值和最小值(只能遍歷一次) |
| 1.A,Demo(案例) |
?Console-算法[]-數(shù)組求最大值和最小值(只能遍歷一次)
| 1.B,Solution(解決方案) |
1.B.2, 方法二
using System; namespace ConsoleApplication1 {class Programe{/// <summary>/// ylb: 算法/// </summary>/// <param name="args"></param>static void Main(string[] args){int max = 0; //假設(shè)最大值是 0int min = 9; //假設(shè)最小值是 9int[] arrayList = new int[] { 3, 5, 7, 1, 8 };//循環(huán)一次foreach (int i in arrayList){//如果 max 小于 i 則,把 i 的值賦予 maxif (max < i)max = i;//如果 min 大于 i 則,把 i 的值賦予 minif (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] 的值賦予 minif (min > array[i]){min = array[i];}//如果 max 小于 array[i] 則,把 array[i] 的值賦予 maxif (max < array[i]){max = array[i];}}Console.WriteLine("max=" + max);Console.WriteLine("min=" + min);}} } View Code| 1.C,Execution Result(運(yùn)行結(jié)果) |
| 作者:ylbtech 出處:http://ylbtech.cnblogs.com/ 本文版權(quán)歸作者和博客園共有,歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接,否則保留追究法律責(zé)任的權(quán)利。 |
總結(jié)
以上是生活随笔為你收集整理的Console-算法[]-数组求最大值和最小值(只能遍历一次)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 实现接口一种可靠的 DLL 接口实现方案
- 下一篇: Redis线程IO模型的秘密知多少