C#——求最大值的索引

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr = new int[] { 1, 3, 5, 3, 5, 2, 66, 32, 4, 444, 32 };
            int max = arr[0];
	    int index = 0;//把假设的最大值索引赋值非index
            for(int i = 1; i < arr.Length; i++)
            {
                if (arr[i] > max)
                {
                    max = arr[i];
		    index = i;//把较大值的索引赋值非index
                }
            }
            Console.WriteLine(max);
	    Console.WriteLine(i);
            Console.ReadLine();
        }
    }
}

猜你喜欢

转载自blog.csdn.net/sndongcheng/article/details/74518186