Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.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/4/algorithm/10.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
Ruby 幂和递归算法_Ruby_Algorithm_Recursion - Fatal编程技术网

Ruby 幂和递归算法

Ruby 幂和递归算法,ruby,algorithm,recursion,Ruby,Algorithm,Recursion,我正在研究一个算法,在这个算法中,我们得到两个输入,一个总量,一个功率值。我们必须找到幂参数的唯一数字组合的总数,求和为总数 例如: 给定数量=10,功率=2,只有一个唯一的解决方案: (1^2)+(3^2)=10 (问题来自这里:) 到目前为止,我的算法如下: def count_unique_combos(total, candidate, power) return -1 if total < 0 || candidate > total # this means impo

我正在研究一个算法,在这个算法中,我们得到两个输入,一个总量,一个功率值。我们必须找到幂参数的唯一数字组合的总数,求和为总数

例如: 给定数量=10,功率=2,只有一个唯一的解决方案: (1^2)+(3^2)=10

(问题来自这里:)

到目前为止,我的算法如下:

def count_unique_combos(total, candidate, power)
  return -1 if total < 0 || candidate > total # this means impossible to continue
  return 1 if total == 0 # arrived at our goal
  num_ways = 0

  # all the ways to get the total amount, if we use candidate ** power
  with_candidate = count_unique_combos(total - candidate ** power, candidate + 1, power)
  num_ways += with_candidate if with_candidate != -1


  # all the ways to get the total amount without using the candidate.
  without_candidate = count_unique_combos(total, candidate + 1, power)
  num_ways += without_candidate if without_candidate != -1

  num_ways
end
def count_unique_组合(总数、候选项、功率)
如果总数<0 | |候选者>总数,则返回-1。这意味着无法继续
如果total==0,则返回1
num_ways=0
#如果我们使用候选人**权力,所有获得总金额的方法
带有\u候选项=计数\u唯一\u组合(总-候选项**功率,候选项+1,功率)
num_ways+=如果与_candidate一起,则与_candidate一起!=-1.
#无需使用候选人即可获得总金额的所有方法。
无候选项=计数唯一的组合(总数,候选项+1,幂)
num_ways+=如果没有_候选者,则没有_候选者!=-1.
数字方式
结束
这就是我困惑的地方。我已经读了很多关于递归算法的leap of faith的书,你假设你的函数适用于N-1个输入,你只需要让它适用于输入大小为N的情况,并放入正确的基本情况

对我来说,基本情况似乎是合理的,递归关系也是合理的(用这个数字获取所有唯一的组合,不用这个数字获取所有组合)


但是,我的输出不正确。对于amount=10和power=2,我的结果值为零。有人知道我在哪里不符合逻辑吗?

尝试交换基本案例

  return 1 if total == 0 # arrived at our goal
  return -1 if total < 0 || candidate > total # this means impossible to continue

当您谈论数字集时,尤其是当涉及到排列和/或组合时,更容易使用核心Ruby函数,如:

然后根据测试用例:

power_sum(10, 2)
# => [[1, 3]]

power_sum(100, 2)
# => [[10], [6, 8], [1, 3, 4, 5, 7]]

power_sum(100, 3)
# => [[1, 2, 3, 4]]
如果您只关心数量,请在末尾调用
.length


这里可以使用递归方法,但处理方法似乎无法正确处理组合。您需要使用两种不同的递归方法来处理N大小的子集。

@Sunny另外,我是否可以建议为
candidate
使用一个默认参数值,这样您就不必每次在初始方法调用时都传递1。嘿@Eli--您是否介意详细说明为什么在初始方法调用中传递1是个坏主意?谢谢每次都通过1似乎是不必要的重复。这不是默认参数的用途吗?
def power_sum(total, power)
  set = (1..total).to_a

  (1..set.length).flat_map do |size|
    set.combination(size).to_a
  end.select do |numbers|
    numbers.inject(0) { |s, n| s + n ** power } == total
  end
end
power_sum(10, 2)
# => [[1, 3]]

power_sum(100, 2)
# => [[10], [6, 8], [1, 3, 4, 5, 7]]

power_sum(100, 3)
# => [[1, 2, 3, 4]]