方法一:
-
#include "stdio.h"
-
#include "stdlib.h"
-
-
-
int findsecondmaxvalue(int *a,int size)
-
{
-
int i,max,s_max;
-
max=a[0];
-
s_max=a[1];
-
for(i=0;i
-
{
-
if(a[i]>max)
-
{
-
s_max=max;
-
max=a[i];
-
}
-
else if(a[i]s_max)
-
s_max=a[i];
-
}
-
return s_max;
-
}
-
-
int main(void)
-
{
-
int second,a[]={111,23,3,5,652,2,3};
-
second=findsecondmaxvalue(a,sizeof(a)/sizeof(a[0]));
-
printf("这个数组中的次大值为:%d\n",second);
-
system("pause");
-
return 0;
-
}
方法二:
-
-
-
-
-
#include "stdio.h"
-
#include "stdlib.h"
-
-
int find(int *a,int n)
-
{
-
int i,second=a[1];
-
for(i=1;i
-
{
-
if(a[i]>second)
-
second=a[i];
-
}
-
return second;
-
}
-
-
int findsecondmaxvalue(int *a,int size)
-
{
-
int i,first,second;
-
first=second=a[0];
-
for(i=1;i
-
{
-
if(a[i]>first)
-
{
-
second=first;
-
first=a[i];
-
}
-
else if(a[i]second)
-
second=a[i];
-
}
-
-
if(first==second)
-
{
-
second=find(a,size);
-
}
-
return second;
-
}
-
-
int main(void)
-
{
-
int a[] = {12012, 3, 45, 5, 66, 232, 65, 7, 8, 898, 56, 878, 170, 13, 5};
-
int second=findsecondmaxvalue(a,sizeof(a)/sizeof(a[0]));
-
printf("这个数组中的次大值为:%d\n",second);
-
system("pause");
-
return 0;
-
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/29012686/viewspace-1149848/
,如需转载,请注明出处,否则将追究法律责任。