思路
遍历一遍即可,但是简单的题目也应该认真对待,使用尽量少的比较次数,如果你 sort 一遍,那就太不合适了...,如果你甚至用了 O(n) 的空间复杂度,也应该反思一下。
#include<iostream> using namespace std; int main(){ int n; while(cin >> n){ int minNum, maxNum; cin >> minNum; maxNum = minNum; for(int i = 1; i < n; i ++){ int num; cin >> num; if(num < minNum) minNum = num; else if(num > maxNum) maxNum = num; } cout << maxNum << " " << minNum << endl; } }