Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 递归以获取数组中所有变量的和_Java_Arrays_Performance_Algorithm_Recursion - Fatal编程技术网

Java 递归以获取数组中所有变量的和

Java 递归以获取数组中所有变量的和,java,arrays,performance,algorithm,recursion,Java,Arrays,Performance,Algorithm,Recursion,我正在尝试编写一个递归方法,允许我对数组中所有可能的整数变量求和 例如,对于具有以下值的数组[1,2,3,4] 结果[1,2]、[1,3]、[1,4]、[2,1]、[2,3]、[3,1]、[3,2]、[3,4]、[4,1]、[4,2]、[4,3]、[1,2,3]、[1,2,4]、[2,1,3]、[2,1,4]等 我对递归一无所知。我的理解是把这个问题分成一个小问题。但我看不出这个案子有什么问题。有人能帮我吗 编辑: 类棒{ 公共整数和(数组列表数组){ int-total=0; 对于(int i

我正在尝试编写一个递归方法,允许我对数组中所有可能的整数变量求和

例如,对于具有以下值的数组[1,2,3,4]

结果[1,2]、[1,3]、[1,4]、[2,1]、[2,3]、[3,1]、[3,2]、[3,4]、[4,1]、[4,2]、[4,3]、[1,2,3]、[1,2,4]、[2,1,3]、[2,1,4]等

我对递归一无所知。我的理解是把这个问题分成一个小问题。但我看不出这个案子有什么问题。有人能帮我吗

编辑:

类棒{
公共整数和(数组列表数组){
int-total=0;
对于(int i=0;i0){print(out);System.out.println(sum(out));}
对于(inti=0;i
静态数组列表置换(int[]a){
ArrayList ret=新的ArrayList();
置换(a,0,ret);
返回ret;
}
公共静态无效置换(int[]arr,int pos,ArrayList list){
如果(arr.length-pos==1){
list.add(arr.clone());
}否则{
对于(int i=pos;i

我想这正是您所需要的。

这里有一个递归方法,它可以工作:

  • 编写一个递归例程
    permute
    ,它包含两个数组:
    arrayIn
    arrayOut
    arrayIn
    包含尚未使用的值,
    arrayOut
    包含部分形成的结果

  • 将使用包含完整值数组的
    arrayIn
    调用程序,
    arrayOut
    将以空开始

  • 在每一步中,您都将通过从
    arrayIn
    中获取值并将其附加到array out中来构建
    arrayOut
    。这是你要找的小箱子。
    arrayIn
    在每个步骤中都会变小,直到到达
    arrayIn
    为空的基本情况

    这是Ruby中的解决方案。如果您不熟悉Ruby语法,我尝试使其可读:

  • 当我们使用数组[1,2,3]运行它时:

        permute([1,2,3], [])
    
    输出为:

    [1]
    total is 1
    [1, 2]
    total is 3
    [1, 2, 3]
    total is 6
    [1, 3]
    total is 4
    [1, 3, 2]
    total is 6
    [2]
    total is 2
    [2, 1]
    total is 3
    [2, 1, 3]
    total is 6
    [2, 3]
    total is 5
    [2, 3, 1]
    total is 6
    [3]
    total is 3
    [3, 1]
    total is 4
    [3, 1, 2]
    total is 6
    [3, 2]
    total is 5
    [3, 2, 1]
    total is 6
    

    你能说得更具体些吗?当你说“所有可能的变体”时,你是什么意思?我很确定你指的是从1到输入数组长度的所有排列,但我不是100%。谢谢你的解决方案。这似乎是我努力实现的目标。。然而,当我用java编写它时。。它似乎停止在迭代的第一级[1]、[1,2]、[1,2,3]、[1,2,3,4]。您能帮我看一下吗?在您的排列例程中,每次从新副本中删除项目并调用
    permute
    之前,您都需要为
    In
    out
    制作一个新副本。
    # function to sum up the values of an array
    def sum(array)
        total = 0
        for elem in array
            total += elem
        end
    
        # return the total
        total
    end
    
    def permute(arrayIn, arrayOut)
        if arrayOut.count > 0
            # print out the array
            puts arrayOut.inspect
    
            # print out the total
            puts "total is #{sum(arrayOut)}"
        end
    
        # For each element in turn in arrayIn...
        for elem in arrayIn
            # remove element from arrayIn, add it to arrayOut, and recurse
            permute(arrayIn - [elem], arrayOut + [elem])
        end
    end
    
        permute([1,2,3], [])
    
    [1]
    total is 1
    [1, 2]
    total is 3
    [1, 2, 3]
    total is 6
    [1, 3]
    total is 4
    [1, 3, 2]
    total is 6
    [2]
    total is 2
    [2, 1]
    total is 3
    [2, 1, 3]
    total is 6
    [2, 3]
    total is 5
    [2, 3, 1]
    total is 6
    [3]
    total is 3
    [3, 1]
    total is 4
    [3, 1, 2]
    total is 6
    [3, 2]
    total is 5
    [3, 2, 1]
    total is 6