求出数组的最大值和次大值
程序员文章站
2022-05-12 10:37:43
...
求整形数组最大值和次大值问题
#include<iostream>
#include<string>
using namespace std;
int main()
{
int a[] = { 23,23,8,2,0,24,12,24 };
int len = sizeof(a) / sizeof(a[0]);
int i, max1 = 0, max2 = 0;
for (i = 0; i < len; i++) {
if (max1 < a[i]) {
max2 = max1;
max1 = a[i];
}
else if (max2 < a[i] && a[i] != max1) {
max2 = a[i];
}
}
printf("max1=%d,max2=%d\n", max1, max2);
}