【问题标题】:Dynamic Programming Change Maker动态规划变革者
【发布时间】:2018-03-13 02:32:55
【问题描述】:

我正在尝试转换以下算法,并且我已经让它大部分工作但是我正在使用的书中有一个示例说我输入了 1、3、4 的面额和 6 的 n 值和接收 2 的输出。

--[[
ALGORITHM ChangeMaking(D[1..m], n)
//Applies dynamic programming to find the minimum number of coins
//of denominations d1< d2 < . . . < dm where d1 = 1 that add up to a
//given amount n
//Input: Positive integer n and array D[1..m] of increasing positive
// integers indicating the coin denominations where D[1]= 1
//Output: The minimum number of coins that add up to n
F[0]←0
for i ←1 to n do
temp←∞; j ←1
while j ≤ m and i ≥ D[j ] do
temp ←min(F [i − D[j ]], temp)
j ←j + 1
F[i]←temp + 1
return F[n]
]]

以下是我尝试转换代码并使其正常工作。我在尝试设置 temp = math.if 时遇到了一些问题2 而是零。

function ChangeMaking(D,n)
--[[
//Applies dynamic programming to find the minimum number of coins
//of denominations d1< d2 < . . . < dm where d1 = 1 that add up to a
//given amount n
//Input: Positive integer n and array D[1..m] of increasing positive
// integers indicating the coin denominations where D[1]= 1
//Output: The minimum number of coins that add up to n
]]
F = {}
m = tablelength(D)
F[0] = 0
  for i =1,n do
    temp = math.inf
    j = 1
    while j <= m and i >= D[j] do
      temp = math.min(F[ i - D[j] ], temp)
      j = j + 1
    end
    F[i] = temp + 1
  return F[n]
  end
end
function main()
  print("Hello Welcome the to Change Maker - LUA Edition")
  print("Enter a series of change denominations, separated by spaces")
  input = io.read()
  deno = {}
   for num in input:gmatch("%d+") do table.insert(deno,tonumber(num)) end
  local i = 1
  while i ~= 0 do
    print("Please Enter Total for Change Maker")
    input2 = io.read("*n")
    if input2 == 0 then i=0 end
    print(ChangeMaking(deno,input2))
  end
end
function tablelength(T)
--[[
//Function for grabbing the total length of a table.
]]
  local count = 0
  for _ in pairs(T) do count = count + 1 end
  return count
end
main()
--[[
    OUTPUT
    Hello Welcome the to Change Maker - LUA Edition
    Enter a series of change denominations, separated by spaces
    1 3 4
    Please Enter Total for Change Maker
    6
    nil
]]

【问题讨论】:

    标签: lua dynamic-programming coin-change


    【解决方案1】:

    return 语句放错地方了。它需要在for 循环之外。在您的版本中,for 循环迭代一次,然后函数返回F[1],即nil

    function ChangeMaking(D, n)
        F = {}
        m = tablelength(D)
        F[0] = 0
        for i = 1, n do
            temp = math.huge
            j = 1
            while j <= m and i >= D[j] do
                temp = math.min(F[ i - D[j] ], temp)
                j = j + 1
            end
            F[i] = temp + 1
        end
        return F[n]
    end
    

    【讨论】:

    • 我可以问你一个附带问题吗?
    • 好的,继续。
    • 您知道我如何跟踪并返回“最佳”集。所以程序现在返回最少的硬币 # 来找到我放的值,但我希望它也返回最佳解决方案,我不知道如何实现。
    • 对不起,我不知道这个函数是如何工作的,更不用说如何让它做更多的事情了。如果我编写了一个函数来做到这一点,那将是完全不同的。我建议你也这样做。
    猜你喜欢
    • 1970-01-01
    • 2011-12-23
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 2018-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多