#include<iostream>
#include<algorithm>
using namespace std;
#define N 5
int main(){
int a[N] = {5,1,3,6,2};
sort(a,a+5);
cout<<a[4]<<" "<<a[3]<<endl;
}
使用打擂台
#include<iostream>
#define N 5
int main(){
int a[N] = {5,1,3,6,2};
int max1 = 0;//最大值
int max2 = 0;//次大值
for(int i=0;i<N;i++){
//数字比最大值还大,将最大值给次大值
if(a[i]>max1){
max2 = max1;
max1 = a[i];
}
else if(a[i]>max2){
max2 = a[i];
}
}
std::cout<<max1<<" "<<max2<<std::endl;
}