一个数组,如:{100,200,300,400}
  输入一个数x,求数组中,小于x的最大值y;
即,当x=350时,y=300

解决方案 »

  1.   


            public static int maxCaculate(int[] array, int x)
            {
                int[] temp = new int[array.Length + 1];
                Array.Copy(array, temp, array.Length);
                temp[temp.Length - 1] = x;
                Array.Sort(temp);
                return temp[Array.IndexOf(temp, x) - 1];
            }
      

  2.   


    public void Find(int[] array, int x)
            { 
                int temp = x;
                foreach (int i in array)
                {
                    if (i >= x)
                        continue;
                    if (temp == x || temp < i)
                        temp = i;
                }
                //未找到就输出x本身
                System.Console.WriteLine(temp);
            }
      

  3.   

    修改一下        public static int maxCaculate(int[] array, int x)
            {
                int[] temp = new int[array.Length + 1];
                Array.Copy(array, temp, array.Length);
                temp[temp.Length - 1] = x;
                Array.Sort(temp);
                if (Array.IndexOf(temp, x) != 0)
                    return temp[Array.IndexOf(temp, x) - 1];
                else
                    return x;
            }