Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/tfs/3.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 解A^4+;B^4+;C^4+;D^4=E^4递归_Java_Arrays_Recursion_Permutation - Fatal编程技术网

Java 解A^4+;B^4+;C^4+;D^4=E^4递归

Java 解A^4+;B^4+;C^4+;D^4=E^4递归,java,arrays,recursion,permutation,Java,Arrays,Recursion,Permutation,你如何写一个算法来找出什么样的正方形组合彼此相等 Asq+bsq+csq+dsq=esq 1+4+9+16=25假太大 1+4+9+16=36假太小不要将所有值提高1;相反,将ABCD视为基数为100的阿拉伯数字(“数字”是整数1-100,1是“零”数字),并编写生成所有此类数字的代码。基本上 将A.B.C.D设置为1.1.1.1 求和,在数组中搜索匹配项 增量D 如果D超过100,则重置为1并增加C 根据需要对B、A重复步骤3-4 当水溢出时停止 停止使用doubles进行整数运算;改用lon

你如何写一个算法来找出什么样的正方形组合彼此相等

Asq+bsq+csq+dsq=esq

1+4+9+16=25假太大


1+4+9+16=36假太小

不要将所有值提高1;相反,将ABCD视为基数为100的阿拉伯数字(“数字”是整数1-100,1是“零”数字),并编写生成所有此类数字的代码。基本上

  • 将A.B.C.D设置为1.1.1.1
  • 求和,在数组中搜索匹配项
  • 增量D
  • 如果D超过100,则重置为1并增加C
  • 根据需要对B、A重复步骤3-4
  • 当水溢出时停止

  • 停止使用
    double
    s进行整数运算;改用
    long
    s
    double
    只能精确表示最多2^54(或类似)的整数,而
    long
    可以表示2^64。除了范围之外,使用双精度可以打开假阴性的大门(ABCD的组合有一个解决方案,但您的代码忽略了它)。

    不要将所有值提高一;相反,将ABCD视为基数为100的阿拉伯数字(“数字”是整数1-100,1是“零”数字),并编写生成所有此类数字的代码。基本上

  • 将A.B.C.D设置为1.1.1.1
  • 求和,在数组中搜索匹配项
  • 增量D
  • 如果D超过100,则重置为1并增加C
  • 根据需要对B、A重复步骤3-4
  • 当水溢出时停止

  • 停止使用
    double
    s进行整数运算;改用
    long
    s
    double
    只能精确表示最多2^54(或类似)的整数,而
    long
    可以表示2^64。除了范围之外,使用双精度可以打开假阴性的大门(ABCD的组合有一个解决方案,但您的代码忽略了它)。

    如果您想使用递归,首先您应该创建一个方法:

  • 将获取整数列表(长)作为输入
  • 检查输入是否与条件匹配
  • 如果与方程式匹配,则返回
  • 否则将增加一个输入值
  • 用增值来称呼自己
  • 所以看起来是这样的:

    private void trySolutionRecursivly(final Long[] values) 
    {
        System.out.println("a = " + values[0] + "; b = " + values[1] + "; c = " + values[2] + "; d = " + values[3] + "; e = " + values[4]);
        if (conditionMet(values))
        {
            System.out.println("Met the condition!");
        }
        else
        {
            Long[] newValues = increaseValues(values);
            trySolutionRecursivly(newValues);
        }   
    }
    
    private void findSolution()
    {
        Long[] newValues = {1L, 1L, 1L, 1L, 1L};
        while (conditionNotMet(newValues))
        {
            newValues = increaseValues(values);
        }
        System.out.println("a = " + values[0] + "; b = " + values[1] + "; c = " + values[2] + "; d = " + values[3] + "; e = " + values[4]);
    }
    
    private Long[] increaseValues(final Long[] values) 
    {
        boolean reindexed = false;
        for (int i = 0; i < values.length; i++)
        {
            if (values[i] == MAX_VALUE)
            {
                if (i > 0)
                {
                    values[i-1]++;
                    reindex(i, values);
                    reindexed = true;
                    break;
                }
                else
                {
                    throw new IllegalStateException("No solution found.");
                }
            }
        }
        if (!reindexed)
        {
            values[values.length-1]++;
        }
        return values;
    }
    
    private Long[] reindex(final Integer startIndex, final Long[] values)
    {
        Long startingValue = values[startIndex - 1];
        for (int i = startIndex; i < values.length; i++)
        {
            values[i] = startingValue;
        }
        return values;
    }
    
    该方法的第一次调用如下所示(一个包含5个1的数组):

    但是如果你试着递归地这样做,你会得到,因为有太多的组合,和太多的递归调用(我已经尝试过)。因此,唯一的解决方案是在循环中顺序调用该方法,即如下所示:

    private void trySolutionRecursivly(final Long[] values) 
    {
        System.out.println("a = " + values[0] + "; b = " + values[1] + "; c = " + values[2] + "; d = " + values[3] + "; e = " + values[4]);
        if (conditionMet(values))
        {
            System.out.println("Met the condition!");
        }
        else
        {
            Long[] newValues = increaseValues(values);
            trySolutionRecursivly(newValues);
        }   
    }
    
    private void findSolution()
    {
        Long[] newValues = {1L, 1L, 1L, 1L, 1L};
        while (conditionNotMet(newValues))
        {
            newValues = increaseValues(values);
        }
        System.out.println("a = " + values[0] + "; b = " + values[1] + "; c = " + values[2] + "; d = " + values[3] + "; e = " + values[4]);
    }
    
    private Long[] increaseValues(final Long[] values) 
    {
        boolean reindexed = false;
        for (int i = 0; i < values.length; i++)
        {
            if (values[i] == MAX_VALUE)
            {
                if (i > 0)
                {
                    values[i-1]++;
                    reindex(i, values);
                    reindexed = true;
                    break;
                }
                else
                {
                    throw new IllegalStateException("No solution found.");
                }
            }
        }
        if (!reindexed)
        {
            values[values.length-1]++;
        }
        return values;
    }
    
    private Long[] reindex(final Integer startIndex, final Long[] values)
    {
        Long startingValue = values[startIndex - 1];
        for (int i = startIndex; i < values.length; i++)
        {
            values[i] = startingValue;
        }
        return values;
    }
    
    现在的诀窍是,正确地增加值

    你的问题意味着会有
    (k+n-1)/(k!(n-1)!
    其中
    k=5
    n=400
    (它不能是100;))所以在我们的例子中它是:
    (5+400-1)/(5!(399)!)=404!/(5!399!)
    400*401*402*403*404/120=87485400080
    可能的解决方案。这相当多,这就是递归在这里不起作用的原因(在最坏的情况下,程序必须存储关于87485400080方法调用的信息)

    现在,与重复的组合对4个值和3个位置的效果如下:

    1;1;1
    1;1;2
    1;1;3
    1;1;4
    1;2;2
    1;2;3
    1;2;4
    1;3;3
    1;3;4
    1;4;4
    2;2;2
    2;2;3
    2;2;4
    2;3;3
    2;3;4
    2;4;4
    3;3;3
    3;3;4
    3;4;4
    4;4;4
    
    正如您所注意到的,每次最后一个索引达到4(最大值),那么第二个到最后一个索引将增加1,并且最后一个索引将设置为与第二个到最后一个索引相同的值。 因此,实现将如下所示:

    private void trySolutionRecursivly(final Long[] values) 
    {
        System.out.println("a = " + values[0] + "; b = " + values[1] + "; c = " + values[2] + "; d = " + values[3] + "; e = " + values[4]);
        if (conditionMet(values))
        {
            System.out.println("Met the condition!");
        }
        else
        {
            Long[] newValues = increaseValues(values);
            trySolutionRecursivly(newValues);
        }   
    }
    
    private void findSolution()
    {
        Long[] newValues = {1L, 1L, 1L, 1L, 1L};
        while (conditionNotMet(newValues))
        {
            newValues = increaseValues(values);
        }
        System.out.println("a = " + values[0] + "; b = " + values[1] + "; c = " + values[2] + "; d = " + values[3] + "; e = " + values[4]);
    }
    
    private Long[] increaseValues(final Long[] values) 
    {
        boolean reindexed = false;
        for (int i = 0; i < values.length; i++)
        {
            if (values[i] == MAX_VALUE)
            {
                if (i > 0)
                {
                    values[i-1]++;
                    reindex(i, values);
                    reindexed = true;
                    break;
                }
                else
                {
                    throw new IllegalStateException("No solution found.");
                }
            }
        }
        if (!reindexed)
        {
            values[values.length-1]++;
        }
        return values;
    }
    
    private Long[] reindex(final Integer startIndex, final Long[] values)
    {
        Long startingValue = values[startIndex - 1];
        for (int i = startIndex; i < values.length; i++)
        {
            values[i] = startingValue;
        }
        return values;
    }
    
    这是正确的答案

    PS
    这实际上是一个好主意,你做了什么-存储在数组中的功率值。在有这么多循环的情况下,这真的很重要。此外,不要尝试打印每一个案例-这会大大降低程序的速度。

    如果要使用递归,首先应该创建一个方法:

  • 将获取整数列表(长)作为输入
  • 检查输入是否与条件匹配
  • 如果与方程式匹配,则返回
  • 否则将增加一个输入值
  • 用增值来称呼自己
  • 所以看起来是这样的:

    private void trySolutionRecursivly(final Long[] values) 
    {
        System.out.println("a = " + values[0] + "; b = " + values[1] + "; c = " + values[2] + "; d = " + values[3] + "; e = " + values[4]);
        if (conditionMet(values))
        {
            System.out.println("Met the condition!");
        }
        else
        {
            Long[] newValues = increaseValues(values);
            trySolutionRecursivly(newValues);
        }   
    }
    
    private void findSolution()
    {
        Long[] newValues = {1L, 1L, 1L, 1L, 1L};
        while (conditionNotMet(newValues))
        {
            newValues = increaseValues(values);
        }
        System.out.println("a = " + values[0] + "; b = " + values[1] + "; c = " + values[2] + "; d = " + values[3] + "; e = " + values[4]);
    }
    
    private Long[] increaseValues(final Long[] values) 
    {
        boolean reindexed = false;
        for (int i = 0; i < values.length; i++)
        {
            if (values[i] == MAX_VALUE)
            {
                if (i > 0)
                {
                    values[i-1]++;
                    reindex(i, values);
                    reindexed = true;
                    break;
                }
                else
                {
                    throw new IllegalStateException("No solution found.");
                }
            }
        }
        if (!reindexed)
        {
            values[values.length-1]++;
        }
        return values;
    }
    
    private Long[] reindex(final Integer startIndex, final Long[] values)
    {
        Long startingValue = values[startIndex - 1];
        for (int i = startIndex; i < values.length; i++)
        {
            values[i] = startingValue;
        }
        return values;
    }
    
    该方法的第一次调用如下所示(一个包含5个1的数组):

    但是如果你试着递归地这样做,你会得到,因为有太多的组合,和太多的递归调用(我已经尝试过)。因此,唯一的解决方案是在循环中顺序调用该方法,即如下所示:

    private void trySolutionRecursivly(final Long[] values) 
    {
        System.out.println("a = " + values[0] + "; b = " + values[1] + "; c = " + values[2] + "; d = " + values[3] + "; e = " + values[4]);
        if (conditionMet(values))
        {
            System.out.println("Met the condition!");
        }
        else
        {
            Long[] newValues = increaseValues(values);
            trySolutionRecursivly(newValues);
        }   
    }
    
    private void findSolution()
    {
        Long[] newValues = {1L, 1L, 1L, 1L, 1L};
        while (conditionNotMet(newValues))
        {
            newValues = increaseValues(values);
        }
        System.out.println("a = " + values[0] + "; b = " + values[1] + "; c = " + values[2] + "; d = " + values[3] + "; e = " + values[4]);
    }
    
    private Long[] increaseValues(final Long[] values) 
    {
        boolean reindexed = false;
        for (int i = 0; i < values.length; i++)
        {
            if (values[i] == MAX_VALUE)
            {
                if (i > 0)
                {
                    values[i-1]++;
                    reindex(i, values);
                    reindexed = true;
                    break;
                }
                else
                {
                    throw new IllegalStateException("No solution found.");
                }
            }
        }
        if (!reindexed)
        {
            values[values.length-1]++;
        }
        return values;
    }
    
    private Long[] reindex(final Integer startIndex, final Long[] values)
    {
        Long startingValue = values[startIndex - 1];
        for (int i = startIndex; i < values.length; i++)
        {
            values[i] = startingValue;
        }
        return values;
    }
    
    现在的诀窍是,正确地增加值

    你的问题意味着会有
    (k+n-1)/(k!(n-1)!
    其中
    k=5
    n=400
    (它不能是100;))所以在我们的例子中它是:
    (5+400-1)/(5!(399)!)=404!/(5!399!)
    400*401*402*403*404/120=87485400080
    可能的解决方案。这相当多,这就是递归在这里不起作用的原因(在最坏的情况下,程序必须存储关于87485400080方法调用的信息)

    现在,与重复的组合对4个值和3个位置的效果如下:

    1;1;1
    1;1;2
    1;1;3
    1;1;4
    1;2;2
    1;2;3
    1;2;4
    1;3;3
    1;3;4
    1;4;4
    2;2;2
    2;2;3
    2;2;4
    2;3;3
    2;3;4
    2;4;4
    3;3;3
    3;3;4
    3;4;4
    4;4;4
    
    正如您所注意到的,每次最后一个索引达到4(最大值),那么第二个到最后一个索引将增加1,并且最后一个索引将设置为与第二个到最后一个索引相同的值。 因此,实现将如下所示:

    private void trySolutionRecursivly(final Long[] values) 
    {
        System.out.println("a = " + values[0] + "; b = " + values[1] + "; c = " + values[2] + "; d = " + values[3] + "; e = " + values[4]);
        if (conditionMet(values))
        {
            System.out.println("Met the condition!");
        }
        else
        {
            Long[] newValues = increaseValues(values);
            trySolutionRecursivly(newValues);
        }   
    }
    
    private void findSolution()
    {
        Long[] newValues = {1L, 1L, 1L, 1L, 1L};
        while (conditionNotMet(newValues))
        {
            newValues = increaseValues(values);
        }
        System.out.println("a = " + values[0] + "; b = " + values[1] + "; c = " + values[2] + "; d = " + values[3] + "; e = " + values[4]);
    }
    
    private Long[] increaseValues(final Long[] values) 
    {
        boolean reindexed = false;
        for (int i = 0; i < values.length; i++)
        {
            if (values[i] == MAX_VALUE)
            {
                if (i > 0)
                {
                    values[i-1]++;
                    reindex(i, values);
                    reindexed = true;
                    break;
                }
                else
                {
                    throw new IllegalStateException("No solution found.");
                }
            }
        }
        if (!reindexed)
        {
            values[values.length-1]++;
        }
        return values;
    }
    
    private Long[] reindex(final Integer startIndex, final Long[] values)
    {
        Long startingValue = values[startIndex - 1];
        for (int i = startIndex; i < values.length; i++)
        {
            values[i] = startingValue;
        }
        return values;
    }
    
    这是正确的答案

    PS
    这实际上是一个好主意,你做了什么-存储在数组中的功率值。在有这么多循环的情况下,这真的很重要。此外,不要试图打印每一个案例-这会大大降低程序的速度。

    使用apache el4j或java中的脚本引擎?@CycDemo:Huh?这有助于OP将迭代算法转化为递归算法吗?使用apache el4j或java中的脚本引擎?@CycDemo:Huh?这是如何帮助OP将迭代算法转化为递归算法的呢?哇,到目前为止这篇文章不错。。。它确实很好地解释了您的解决方案!继续努力!哇好帖子到目前为止。。。这真的解释了