以下为我自己的代码
#include <iostream>
using namespace std;
int getMaxOrMin(int *arr,int count,bool isMax){
int tempMax = arr[0];
int tempMin = arr[0];
int i;
for(i = 0;i<count;i++){
if(tempMax<arr[i]) tempMax = arr[i];
if(tempMin>arr[i]) tempMin = arr[i];
}
if(isMax) return tempMax;
else return tempMin;
}
int main() {
int count;
cout <<"请输入数组长度:"<<endl;
cin >>count;
int arr[count];
int i;
for(i = 0;i<count;i++){
cout <<"请输入第"<<i+1<<"个数"<<endl;
cin >>arr[i];
}
cout <<"请输入指令:1.获得最大值 0.获得最小值"<<endl;
bool isFlag;
cin >>isFlag;
int temp = getMaxOrMin(arr,count,isFlag);
cout <<(isFlag?"最大值":"最小值")<<"为:"<<temp;
return 0;
}