题解 | #互换最大最小数#
互换最大最小数
https://www.nowcoder.com/practice/04c0f21530994fbfa94887f1e3588e6a
#include <iostream> #include <limits.h> using namespace std; int main () { int n; int arr[21]; int x = 0, y = 0; while (cin >> n){ for (int i = 0; i < n; i++){ cin >> arr[i]; } int maxNum = arr[0]; int minNum = arr[0]; for (int i = 1; i < n; i++){ if (arr[i] > maxNum){ maxNum = arr[i]; x = i; }else if (arr[i] < minNum){ minNum = arr[i]; y = i; }else{ continue; } } swap (arr[x], arr[y]); for (int i = 0; i < n; i++){ cout << arr[i] << " "; } cout << endl; } return 0; }