24 奇数下标都是奇数或者偶数下标都是偶数
Table of Contents

ac(交换思路)

import java.util.Scanner;


public class Main {

    public static Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        int n = sc.nextInt();
        int[] arr = new int[n];
        for(int i = 0; i < n; i ++){
            arr[i] = sc.nextInt();
        }
        int evenIndex = 0;
        int oddIndex = 1;
        int endIndex = n - 1;
        while (evenIndex <= endIndex && oddIndex <= endIndex) {
            // 最后位置是偶数, 交换偶数下标
            if(arr[endIndex] % 2 == 0){
                int tmp = arr[endIndex];
                arr[endIndex] = arr[evenIndex];
                arr[evenIndex] = tmp;
                evenIndex += 2;
            }else {
                // 最后位置是奇数则交换奇数下标
                int tmp = arr[endIndex];
                arr[endIndex] = arr[oddIndex];
                arr[oddIndex] = tmp;
                oddIndex += 2;
            }
        }
        for(int i=0;i<n-1;i++){
            System.out.print(arr[i] + " ");
        }
        System.out.println(arr[n-1]);
    }

}