整型数组正序、逆序输出(索引)(交叉排序)

整型数组正序、逆序输出(索引)(交叉排序)_第1张图片
整型数组正序、逆序输出(索引)(交叉排序)_第2张图片

import java.util.Arrays;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        int l1 = scan.nextInt(),r1 = scan.nextInt(),l2 = scan.nextInt(),r2 = scan.nextInt();
        int[] a = new int[n+1];
        for(int i = 0; i < n; i ++) {
            a[i] = scan.nextInt();
        }

        Arrays.sort(a, l1-1, r1);   
        Arrays.sort(a, l2-1, r2);

        for(int i = 0; i < (r2-l2+1)/2; i ++) {
            int tmp = a[l2-1+i];
            a[l2-1+i] = a[r2-1-i];
            a[r2-1-i] = tmp;
        }
        for(int i = 0; i < n; i ++) {
            System.out.print(a[i]);
            if(i!=n-1)
                System.out.print(" ");
        }
    }
}

你可能感兴趣的:(水题)