输出数组中第二大数
2013-04-13 18:44
239 查看
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(); } }
相关文章推荐
- C++通过自定义函数找出一个整数数组中第二大数的方法
- 找出数组中第二大数
- // 对于一个数组返回第二大数
- 寻找数组中第二大数
- Z型输出数组 ------ccf认证第二题
- C语言求数组的第二大数
- 寻找数组中第二大数
- 数据结构——算法之(034)(编写一个函数求一个数组中的第二大数)
- 一次遍历找出整型数组中第二大数
- 输出最大数和第二大数
- 简单的方式寻址数组中第二大数的方式
- 如何求数组中的最大数和第二大数
- 快速找出一个数组中的最大数、第二大数。
- 寻找数组第二大数和第K大数
- [基础题]1.快速找出一个数组中的最大数、第二大数。
- 找出整数数组中第二大数
- 写一个函数,找出一个整数数组中的第二大数
- 一个数组中第一大数和第二大数
- 快速找出数组中的最大数和第二大数
- 找数组中的第二大数