题解 | #互换最大最小数#
互换最大最小数
https://www.nowcoder.com/practice/04c0f21530994fbfa94887f1e3588e6a
#include <stdio.h> #include <stdlib.h> int main() { int a[25], n; while (scanf("%d", &n) != EOF) { int minnum, maxnum; scanf("%d", &a[0]); minnum = a[0]; maxnum = a[0]; int min_index=0; int max_index=0; for (int i = 1; i < n; i++) { scanf("%d", &a[i]); if (a[i] < minnum) { minnum = a[i]; min_index = i; } else if (a[i] > maxnum) { maxnum = a[i]; max_index = i; } } int temp = a[min_index]; a[min_index] = a[max_index]; a[max_index] = temp; for (int i = 0; i < n; i++) { printf("%d ", a[i]); } printf("\n"); } return 0; }