Advertisement
wym1111

Untitled

Dec 2nd, 2023
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.75 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int N = 110;
  5. int n, a[N];
  6.  
  7. int main(){
  8.     cin >> n;
  9.     for (int i = 1; i <= n; i ++) {
  10.         cin >> a[i];
  11.     }
  12.     for (int i = 1, j = n; i < j; i ++, j --) {
  13. //      cout << i << ' ' << j << endl;
  14.         int minIndex = i;
  15.         for (int k = i; k <= j; k ++) {
  16.             if (a[minIndex] > a[k]) {
  17.                 minIndex = k;
  18.             }
  19.         }
  20.         swap(a[minIndex], a[i]);
  21.         for (int k = 1; k <= n; k ++) {
  22.             cout << a[k] << ' ';
  23.         }
  24.         cout << endl;
  25.         if (i + 1 == j) break; // 如果 i,j已经相邻
  26.         int maxIndex = j;
  27.         for (int k = j; k > i; k --) {
  28.             if (a[maxIndex] < a[k]) {
  29.                 maxIndex = k;
  30.             }
  31.         }
  32.         swap(a[maxIndex], a[j]);
  33.         for (int k = 1; k <= n; k ++) {
  34.             cout << a[k] << ' ';
  35.         }
  36.         cout << endl;
  37.     }
  38.     return 0;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement