如何使用一次for循环得到数组中第二大的数

Posted github_white

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何使用一次for循环得到数组中第二大的数相关的知识,希望对你有一定的参考价值。

装载声明:http://blog.csdn.net/lxsmk9059/article/details/77920206?locationNum=1&fps=1

 1 int array[] = {1,12,58,369,45,17,59,3654,370};
 2     int max = array[0];
 3     int secondmax = array[0];
 4 
 5     for(int i = 0; i < sizeof(array)/sizeof(int); i++)
 6     {
 7         if(array[i] > max)
 8         {
 9             secondmax = max;
10             max = array[i];
11         }
12         else if(array[i] > secondmax)
13         {
14             secondmax = array[i];
15         }
16     }

 

以上是关于如何使用一次for循环得到数组中第二大的数的主要内容,如果未能解决你的问题,请参考以下文章

求出整形数组中第二大的数的值

读入一组整数到数组中,设计一程序,找出最大偶数和最大奇数并输出

找出一个整形数组中第二大的数字

找数组中第K大的数 请教

用Java找出一个整数数组中最大的值,并返回最大值的数组下标,尤其是有两个或是三个最大值时

求一个一维数组中第二大的元素,及其在原数组中的下标。