【问题标题】:Recursive number permutation in JavaJava中的递归数字排列
【发布时间】:2019-02-13 23:20:10
【问题描述】:

最近我被分配了一个这样的作业:

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

public static void permutation(int num)

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

按升序显示这些值。例如,如果长度为 3,您的程序应显示:

111
113
115
117
119
131
133
135
...

我对 Java 很陌生(过去一年我之前的所有编码任务都是在 Python 中完成的,这是我从事的第三个 Java 程序),所以我完全不知道从哪里获得开始了。如果有人能帮助我并告诉我如何开始,我将不胜感激,谢谢。

【问题讨论】:

    标签: java recursion combinations permutation cartesian-product


    【解决方案1】:

    虽然我不喜欢“帮我做作业”的问题,但这次我会提供答案。这是我的建议。请不要只是复制粘贴上交。先了解一下,通过这个例子了解一些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 的事实使这个解决方案毫无用处,但如果任务只是证明您了解递归,那应该就足够了。

    【讨论】:

      【解决方案2】:

      mapreduce 方法

      您可以将此任务解释为获取n-th 个奇数数字 数组的笛卡尔积。如果是n=3,那么你会得到5<sup>3</sup>=125 组合。

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

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

      reduce 方法将 2D 数组对相加成一个 2D 数组 - 如果 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?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-08-24
        • 1970-01-01
        • 1970-01-01
        • 2015-03-07
        • 1970-01-01
        • 2014-08-19
        • 2018-08-07
        • 1970-01-01
        相关资源
        最近更新 更多