新开两个变量,然后写个if的分支结构与a[i]比较,保证最大或最小,输出它们就可以了。(
CODE:
#include<bits/stdc++.h>
using namespace std;
const int maxn = 100001;
int a[maxn];
int main(){
int n;
int min_num, max_num;
cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i];
for(int i = 1; i <= n; i++){
if(i==1) min_num = a[i];
if(a[i] < min_num){
min_num = a[i];
}
}
for(int i = 1; i <= n; i++){
if(i==1) max_num = a[i];
if(a[i] > max_num){
max_num = a[i];
}
}
cout << min_num << " " << max_num;
return 0;
}