#include<iostream> using namespace std; int secondMax(int arr[],int n) { int max1,max2; int i; max1=max2=0; for(i=0;i<n;i++) { if(arr[i]<max2) continue; else if(arr[i]<max1) max2=arr[i]; else { max2=max1; max1=arr[i]; } } return max2; } int main() { int array[]={3,6,2,7,8,5,4}; int second=secondMax(array,sizeof(array)/sizeof(array[0])); cout<<second<<endl; return 0; }
运行结果
[root@localhost Recursion]# g++ -o secondMax secondMax.cpp [root@localhost Recursion]# ./secondMax 7