您的位置:首页 > 其它

输出数组中第二大数

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();

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: