zoukankan      html  css  js  c++  java
  • 求给定数组中最大值和其在数组中的索引并输出

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Collections;
    
    
    namespace _03
    {
        class Program
        {
            static void Main(string[] args)
            {
                //求给定数组中最大值和其在数组中的索引并输出
                int[] intArray = { 1, 4, 34, 23, 45, 45, 23, 78, 12, 78 };  //int数组
                List<int> index = new List<int>();//声明集合 用于存放最大值的索引
                int max = 0;//存最大值
                max = CheckMax(intArray, out  index);//调用方法查找最大值和其索引
                 Console.Write("最大值是:{0},其在数组中的索引是", max);
                foreach (int item in index)//遍历最大值所在数组的索引
                {
                    Console.Write("{0}	", item);
                }
                Console.ReadKey();
            }
            /// <summary>
            /// 找出给定int数组中最大值和最大值的索引
            /// </summary>
            /// <param name="intArray">数组</param>
            /// <param name="index">存最大值在数组中的索引</param>
            /// <returns>最大值  最大值的索引</returns>
            private static int CheckMax(int[] intArray, out List<int> index)
            {
                index = new List<int>();//为index赋初值,   注:用out修饰的参数在使用前必须对其赋值
                int max = 0;
                for (int i = 0; i < intArray.Length; i++)
                {
                    if (max < intArray[i])//如果有大于当前所得最大值,清空list,并将该值赋给max
                    {
                        index.Clear();
                        max = intArray[i];
                        index.Add(i);
                    }
                    else if (max == intArray[i])//重复最大值时,向list中添加其索引
                    {
                        index.Add(i);
                    }
                }
                return max;
            }
        }
    }
    
  • 相关阅读:
    Cannot find a free socket for the debugger
    如何让myeclipse左边选中文件后自动关联右边树
    myeclipse反编译安装 jd-gui.exe下载
    MyEclipse报错Access restriction: The type BASE64Encoder is not accessible due to restriction on required library
    如何使用JAVA请求HTTPS
    如何使用JAVA请求HTTP
    SVN提交代码时报405 Method Not Allowed
    对称加密和非对称加密
    ORA-12505, TNS:listener does not currently know of SID given in connect descriptor
    调整Linux操作系统时区-centos7
  • 原文地址:https://www.cnblogs.com/dfyg-xiaoxiao/p/7213787.html
Copyright © 2011-2022 走看看