solution.java 1.3 KB
Newer Older
每日一练社区's avatar
每日一练社区 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
import java.util.Arrays;

public class Text13 {

    static int h = 0;

    public static void main(String[] args) {
        int[] array = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
        permute(array, 0);
    }

    // 验证
    public static int[] text(int[] array) {
        boolean a = array[0] < array[1] && array[0] < array[2];
        boolean b = array[1] < array[3] && array[1] < array[4];
        boolean c = array[2] < array[4] && array[2] < array[5];
        boolean d = array[3] < array[6] && array[3] < array[7];
        boolean e = array[4] < array[7] && array[4] < array[8];
        boolean f = array[5] < array[8] && array[5] < array[9];
        if (a & b & c & d & e & f) {
            return array;
        }
        return null;
    }

    // 递归排列
    public static void permute(int[] array, int start) {
        if (start == array.length - 1 && text(array) != null) {
            System.out.println(++h + " " + Arrays.toString(text(array)));
        }
        for (int i = start; i < array.length; i++) {
            swap(array, start, i);
            permute(array, start + 1);
            swap(array, start, i);
        }
    }

    // 交换
    public static void swap(int[] array, int start, int end) {
        int telp = array[start];
        array[start] = array[end];
        array[end] = telp;
    }
}