#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int i, j, k = 0, max, nextMax;
int a[20];
while(k != 20)
{
scanf("%d", &a[k]);
++k;
}
max = nextMax = a[0];
for(i = 0; i < 20; ++i)
{
nextMax = (nextMax > (max < a[i] ? max : a[i]) ? nextMax : (max < a[i] ? max : a[i]));
max = (max > a[i] ? max : a[i]);
if(nextMax == max)
{
nextMax = a[i];
}
}
printf("max = %d, nextMax = %d\n", max, nextMax);
system("pause");
}
第一次没加if判断,此情况下若数组第一个数就是最大值,那么最后最大值和次大值的数相同,都为最大值。
求数组最大次大值
最新推荐文章于 2022-09-15 22:36:22 发布