【问题标题】:Cartesian power - via recursion笛卡尔幂 - 通过递归
【发布时间】:2019-03-31 10:59:59
【问题描述】:

原来的问题在这里:Cartesian power (a special Cartesian product) -- choose elements from array, in repeatable style

在旧问题中,已经有答案通过迭代给出了解决方案。

我想知道是否有 recursive 解决方案,类似于以下链接中的解决方案,它使用递归打印排列:https://www.geeksforgeeks.org/write-a-c-program-to-print-all-permutations-of-a-given-string/

目前我写了以下程序,但还不正确,有什么帮助吗?


代码 - 实现

CartesianPowerRecursive.java:

import java.util.Arrays;

/**
 * Print cartesian power of array elements, by recursion.
 * 
 * @author eric
 * @date Oct 13, 2018 12:28:10 PM
 */
public class CartesianPowerRecursive {
    public static int cartesianPower(int arr[]) {
        int tmpArr[] = Arrays.copyOf(arr, arr.length);
        return cartesianPower(arr, tmpArr, 0, 0);
    }

    private static int cartesianPower(int arr[], int tmpArr[], int n, int m) {
        // FIXME ... not correct yet,

        int counter = 0;
        for (int i = n; i < arr.length; i++) {
            for (int j = m; j < arr.length; j++) {
                tmpArr[j] = arr[i];
                counter += cartesianPower(arr, tmpArr, n, j + 1);
            }
        }

        if (m == arr.length - 1) {
            counter++;
            System.out.println(Arrays.toString(tmpArr));
        }

        return counter;
    }
}

代码 - 测试用例

(通过 TestNG)

CartesianPowerRecursiveTest.java:

import org.testng.Assert;
import org.testng.annotations.Test;

/**
 * CartesianPowerRecursive test.
 * 
 * @author eric
 * @date Oct 26, 2018 11:45:27 PM
 */
public class CartesianPowerRecursiveTest {
    @Test
    public void test() {
        int arr[] = new int[] { 0, 1, 2 };
        Assert.assertEquals(CartesianPowerRecursive.cartesianPower(arr), 27);
    }
}

【问题讨论】:

    标签: algorithm math recursion permutation cartesian-product


    【解决方案1】:

    递归的方法很简单。伪代码(不知道如何处理 Java 静态/非静态等):

    编辑:制作if (m &lt; 0)

    public static void cartesianPower(int arr[], int tmpArr[], int n, int m){
        if (m < 0)
           System.out.println(Arrays.toString(tmpArr));
        else
          for (int i = 0; i < n; i++) {
            tmpArr[m] = arr[i];
            cartesianPower(arr, tmpArr, n, m - 1);
          }
    }
    

    WorkingPython代码:

    def cartesianPower(arr, tmpArr, n, m):
        if (m < 0):
            print(tmpArr)
        else:
            for i in range(n):
                tmpArr[m] = arr[i]
                cartesianPower(arr, tmpArr, n, m - 1)
    
    arr = [0,1,2]
    tmpArr = [0,0,0]
    cartesianPower(arr, tmpArr, len(arr), len(arr) - 1)
    

    Version with lexicographic ordering

    【讨论】:

    • 我复制了方法并替换了原来的方法,但结果不正确,它只打印原始数组,并返回 count = 1。也许您可以提供任何语言的代码可执行,谢谢。
    • lexicographic ordering 版本很好,我已在原始问题的回答中将其翻译成 Java 版本:stackoverflow.com/a/52989556 (CartesianPower.java -> cartesianPowerViaRecursion() 是 impl,带有测试用例CartesianPowerRecursiveRawTest.java 和 CartesianPowerRecursiveTest.java)。顺便说一句,似乎代码的cartesianPower() 中的第三个参数n 不是必需的,因为它只是数组长度,并且可以从任何一个数组参数本身获得,我的问题中的代码有点误导。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-08
    • 1970-01-01
    • 1970-01-01
    • 2020-03-01
    • 2018-07-06
    • 2012-09-07
    • 2012-05-07
    相关资源
    最近更新 更多