#include <stdio.h> #include <stdlib.h> #include <time.h> #define N 10 // 找出数组中第二个最大的数 int findSecMax(int arr[], int n) { const int MINNUMBER = -32767; int sec_max, max; sec_max = MINNUMBER; max = arr[0]; for (int i = 1; i < n; i++) { if (arr[i]> max) { sec_max = max; max = arr[i]; } else { if (arr[i] > sec_max) { sec_max = arr[i]; } } } return sec_max; } void main() { int arr[N], i,sec_max; srand((unsigned int)time(NULL)); for (i = 0; i < N;i++) { arr[i] = rand() % 100; printf("%4d", arr[i]); } sec_max = findSecMax(arr, N); printf("\n第二个最大的数为:%d\n",sec_max); system("pause"); }
时间: 2024-09-24 01:35:38