using System;
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr =new int[9]{ 0,1, 3, 4, 5, 6, 7, 10, 8 };
            maxMNUM pt = new maxMNUM();
            int cw=pt.maxMN(arr);
            Console.WriteLine(cw);
            Console.ReadKey();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication1
{
    class maxNUM
    {
       // int a, b;
       //int temp;
        public int maxN( int a, int b)
        {
           // int temp;
            if (a > b)
            {
                return a;
            }
            else 
            {
                return b;
            }
        }
    }
}using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;namespace ConsoleApplication1
{
    class maxMNUM
    {
        
        maxNUM p = new maxNUM();
        
        public int maxMN(int[] n)
        {
            int[] t =new int[n.Length];
            int c = n.Length;
            if (c == 2)
            {
               /* if (n[0] > n[1])
                {
                    return n[0];
                }
                else
                {
                    return n[1];
                }*/
               return p.maxN(n[0], n[1]);            }
            else
            {
               // return 0;
                t[c-1] = n[c - 1];
                ArrayList al = new ArrayList(n);
                al.RemoveAt(c - 1);
                n = (int[])al.ToArray(typeof(int));
                c = c - 1;
                return p.maxN(maxMN(n), t[c]);
                
            
            }
        }
    }
}
菜鸟C#编写人员,求高手指点,谢谢(上述本人用的是递归思想——前N-1中的最大与第N的比较)

解决方案 »

  1.   

    int[] arr =new int[9]{ 0,1, 3, 4, 5, 6, 7, 10, 8 };
    int max=arr[0];
    int l=0;
    for(i=1;i<9;i++)
    {
      if(max<arr[i])
      {
         max=arr[i];
         l=i;
      }
    }
    Console.WriteLine("最大数为{0},下标为{1}",max,l);
    Console.ReadLine();//输出为:最大数为10,下标为7
    //因为数组的下标是从0开始计算,所以10的下标为7
      

  2.   


    这只是为了让大家能更好的看清楚,一般使用时是这样用的:
    int[] arr =new int[9]{ 0,1, 3, 4, 5, 6, 7, 10, 8 }; 
    int max=arr[0]; 
    int l=0; 
    for(i=1;i <arr.length()-1;i++) 

      if(max <arr[i]) 
      { 
        max=arr[i]; 
        l=i; 
      } 

    Console.WriteLine("最大数为{0},下标为{1}",max,l); 
    Console.ReadLine();