Java程序以显示整数数组的排列

时间:2019-03-29 20:43:18

标签: java recursion permutation

试图使用递归来显示int数组的排列,但是由于某种原因,当我调用该函数时,它不会显示任何内容。我缺少的主要功能中有东西吗?

     public static void Permutation(int[] a, int prefix) {

    int length = a.length;

    if (length == prefix) {

        printArray(a);

    }

    else {

        for (int i = 0; i < prefix; i++) {

            swap(a, prefix, i);

            Permutation(a, prefix + 1);

            swap(a, prefix, i);

        }
    }
}

public static void swap(int[] a, int x, int y) {

    int z = a[x];

    a[x] = a[y];

    a[y] = z;

}

public static void printArray(int[] thing) {

    System.out.println("\n");

    for (int x = 0; x < thing.length; x++)
        System.out.print(thing[x]);

}

public static void main(String[] args) {

    int a[] = { 1, 2, 3 };
            Permutation(a, 0);
    }

说我有一个1、2和3的int数组。输出应该是

1 2 3
1 3 2 
2 1 3 
2 3 1
3 1 2 
3 2 1

1 个答案:

答案 0 :(得分:0)

查看您的for循环并分析程序:)

$messages = $mysqli->query('SELECT `message` FROM `history` LIMIT 0,100;');
while($row = $messages->fetch_array(MYSQLI_ASSOC)) {
    $msg = $row['message'];
    $result2 = $mysqli->query(
        'SELECT `id` FROM `messages` WHERE `message` = "'.$msg.'" LIMIT 0,1'
    );
    $row2 = $result2->fetch_array(MYSQLI_ASSOC);
    $id = $row2['id'];
    $mysqli->query(
        'UPDATE `history` SET `message`="'.$id.'" WHERE `message` = "'.$msg.'"'
    );
    $result2->free_result();
}