Java中的递归数置换

时间:2018-09-09 07:22:42

标签: java recursion permutation

最近我被分配了一个作业,内容如下:

编写一个名为NumberPermutation.java的程序并实现以下指定的递归方法:

公共静态无效置换(整数)

此方法的参数为正整数,并显示该长度的奇数位的所有排列。

以升序显示这些值。例如,如果长度为3,则程序应显示:

111 113 115 117 119 131 133 135

我对Java还是很陌生(因为过去一年中所有以前的编码工作都使用Python,这是我研究的第三个Java程序),所以我绝对不知道该去哪儿开始。如果有人可以伸出援手告诉我如何入门,谢谢。

2 个答案:

答案 0 :(得分:0)

尽管我不喜欢闻到“帮助我完成作业”的问题,但这次我会提供答案。这是我的建议。请不要只是复制,粘贴和上交。首先要理解,并使用此示例来了解Java的一些基础知识。

public class RecursionTester {

public static void main(String[] args) {
    new RecursionTester().permutation(3);
}

private void permutation(int num) {
    permutation(num, "");
}

private void permutation(int num, String buffer) {
    if (num == 0) {
        System.out.println(buffer);
        return;
    }
    for (int i = 1; i < 10; i += 2) {
        permutation(num - 1, buffer + Integer.toString(i));
    }
}

}

结果仅存入System.out的事实使该解决方案变得毫无用处,但是如果任务是仅证明您了解递归,那应该就足够了。

答案 1 :(得分:0)

mapreduce 方法

您可以将此任务解释为获取第 n 个奇数数字二维数组的笛卡尔积。如果 n=3 那么你得到 53=125 个组合。

mapToObj 方法为求和准备二维数组并指向结果的格式:

1: {{1}, {3}, {5}, {7}, {9}}
2: {{1}, {3}, {5}, {7}, {9}}
3: {{1}, {3}, {5}, {7}, {9}}

reduce 方法将成对的二维数组相加到单个二维数组中 - 如果 n=3 的两个减少步骤:

1: {{1}, {3}, {5}, {7}, {9}}
2: {{1}, {3}, {5}, {7}, {9}}
---
sum1: {{1, 1}, {1, 3}, {1, 5}, ...}
3: {{1}, {3}, {5}, {7}, {9}}
---
total: {1, 1, 1}, {1, 1, 3}, ...}

Try it online!

public static int[][] cartesianProduct(int n, int[][] arr) {
    return IntStream.range(0, n)
            // stream of n-th number of 2D arrays
            .mapToObj(i -> arr)
            // stream of 2D arrays into a single 2D array
            .reduce((arr1, arr2) -> Arrays.stream(arr1)
                // combinations of rows of two arrays
                .flatMap(row1 -> Arrays.stream(arr2)
                    // concatenate into a single row
                    .map(row2 -> Stream.of(row1, row2)
                        .flatMapToInt(Arrays::stream)
                        // row of a 2D array
                        .toArray()))
                // 2D array of combinations
                .toArray(int[][]::new))
            // otherwise an empty array
            .orElse(new int[0][]);
}
public static void main(String[] args) {
    // exponent
    int n = 3;
    // 2D array of odd digits
    int[][] arr = {{1}, {3}, {5}, {7}, {9}};
    // cartesian product
    int[][] cp = cartesianProduct(n, arr);
    // output
    System.out.println("Number of combinations: " + cp.length);
    // number of rows
    int rows = cp.length / arr.length;
    // column-wise output
    IntStream.range(0, rows)
            .mapToObj(i -> IntStream.range(0, cp.length)
                .filter(j -> j % rows == i)
                .mapToObj(j -> Arrays.toString(cp[j]))
                .collect(Collectors.joining(" ")))
            .forEach(System.out::println);
}

输出:

Number of combinations: 125
[1, 1, 1] [3, 1, 1] [5, 1, 1] [7, 1, 1] [9, 1, 1]
[1, 1, 3] [3, 1, 3] [5, 1, 3] [7, 1, 3] [9, 1, 3]
[1, 1, 5] [3, 1, 5] [5, 1, 5] [7, 1, 5] [9, 1, 5]
[1, 1, 7] [3, 1, 7] [5, 1, 7] [7, 1, 7] [9, 1, 7]
[1, 1, 9] [3, 1, 9] [5, 1, 9] [7, 1, 9] [9, 1, 9]
[1, 3, 1] [3, 3, 1] [5, 3, 1] [7, 3, 1] [9, 3, 1]
[1, 3, 3] [3, 3, 3] [5, 3, 3] [7, 3, 3] [9, 3, 3]
[1, 3, 5] [3, 3, 5] [5, 3, 5] [7, 3, 5] [9, 3, 5]
[1, 3, 7] [3, 3, 7] [5, 3, 7] [7, 3, 7] [9, 3, 7]
[1, 3, 9] [3, 3, 9] [5, 3, 9] [7, 3, 9] [9, 3, 9]
[1, 5, 1] [3, 5, 1] [5, 5, 1] [7, 5, 1] [9, 5, 1]
[1, 5, 3] [3, 5, 3] [5, 5, 3] [7, 5, 3] [9, 5, 3]
[1, 5, 5] [3, 5, 5] [5, 5, 5] [7, 5, 5] [9, 5, 5]
[1, 5, 7] [3, 5, 7] [5, 5, 7] [7, 5, 7] [9, 5, 7]
[1, 5, 9] [3, 5, 9] [5, 5, 9] [7, 5, 9] [9, 5, 9]
[1, 7, 1] [3, 7, 1] [5, 7, 1] [7, 7, 1] [9, 7, 1]
[1, 7, 3] [3, 7, 3] [5, 7, 3] [7, 7, 3] [9, 7, 3]
[1, 7, 5] [3, 7, 5] [5, 7, 5] [7, 7, 5] [9, 7, 5]
[1, 7, 7] [3, 7, 7] [5, 7, 7] [7, 7, 7] [9, 7, 7]
[1, 7, 9] [3, 7, 9] [5, 7, 9] [7, 7, 9] [9, 7, 9]
[1, 9, 1] [3, 9, 1] [5, 9, 1] [7, 9, 1] [9, 9, 1]
[1, 9, 3] [3, 9, 3] [5, 9, 3] [7, 9, 3] [9, 9, 3]
[1, 9, 5] [3, 9, 5] [5, 9, 5] [7, 9, 5] [9, 9, 5]
[1, 9, 7] [3, 9, 7] [5, 9, 7] [7, 9, 7] [9, 9, 7]
[1, 9, 9] [3, 9, 9] [5, 9, 9] [7, 9, 9] [9, 9, 9]

另见:How do I generate a Cartesian product in Java?