class Program
    {
        private static int get2rdMax(int[] ar)
        {
            int max = ar[1], s_max = ar[1];
            for (int i = 0; i < ar.Length; i++)
            {
                if (ar[i] > s_max)
                {
                    s_max = ar[i];
                    if (s_max > max)
                    {
                        max += s_max;
                        s_max = max - s_max;
                        max -= s_max;
                    }
                }
            }

            if (max == s_max)
                throw new Exception("no second max!");
            else
                return s_max;
        }

        static void Main(string[] args)
        {
            int[] ar = { 1, 2, 3, 4, 5, 6 };
            try
            {
                Console.WriteLine(get2rdMax(ar).ToString());
            }
            catch (Exception exc)
            {
                Console.WriteLine(exc.Message);
            }
            Console.ReadKey();

        }
    }

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-05-22
  • 2021-12-12
  • 2021-09-21
  • 2021-06-20
  • 2022-12-23
猜你喜欢
  • 2021-10-02
  • 2021-12-17
  • 2021-10-24
  • 2021-05-29
  • 2022-02-27
相关资源
相似解决方案