这个任务旨在实现一个原始计算器的动态编程方法,该计算器只能加1,乘以2并乘以3.因此,输入n确定达到n的最小操作数。我已经实现了一个非常天真的dp或我认为是dp方法。它不起作用。我没有其他人要问。对于n = 5的输入,以下输出为:([0,1,2,2,3,4],[1,1,2,3,4,5])而有两个正确的输出列表编号= [1,2,4,5]或[1,3,4,5]。一些帮助将不胜感激。
def DPmin_operations(n):
numbers = []
minNumOperations = [0]*(n+1)
numOps = 0
numbers.append(1)
for k in range(1,n+1):
minNumOperations[k] = 10000
# for *3 operator
if k % 3 == 0:
numOps = minNumOperations[k//3] + 1
if numOps < minNumOperations[k]:
minNumOperations[k] = numOps
numbers.append(k)
# for *2 operator
elif k % 2 == 0:
numOps = minNumOperations[k//2] + 1
if numOps < minNumOperations[k]:
minNumOperations[k] = numOps
numbers.append(k)
# for + 1 operator
elif k >= 1:
numOps = minNumOperations[k - 1] + 1
if numOps < minNumOperations[k]:
minNumOperations[k] = numOps
numbers.append(k)
return (minNumOperations, numbers)
答案 0 :(得分:4)
请注意,elif
块应该是if
块。目前,您正在使用总是试图除以3的贪婪算法;如果失败了,那就试着除以2;如果失败,则减去1.可能数字可被6整除,因此所有三个选项都是可能的,但除以2则更优,然后除以3.
至于获取你的数字列表,最后这样做。存储所有可能的父母,然后从目标向后工作,看看你是如何到达那里的。
def dp_min_ops(n):
all_parents = [None] * (n + 1)
all_min_ops = [0] + [None] * n
for k in range(1, n + 1):
curr_parent = k - 1
curr_min_ops = all_min_ops[curr_parent] + 1
if k % 3 == 0:
parent = k // 3
num_ops = all_min_ops[parent] + 1
if num_ops < curr_min_ops:
curr_parent, curr_min_ops = parent, num_ops
if k % 2 == 0:
parent = k // 2
num_ops = all_min_ops[parent] + 1
if num_ops < curr_min_ops:
curr_parent, curr_min_ops = parent, num_ops
all_parents[k], all_min_ops[k] = curr_parent, curr_min_ops
numbers = []
k = n
while k > 0:
numbers.append(k)
k = all_parents[k]
numbers.reverse()
return all_min_ops, numbers
print(dp_min_ops(5)) # ([0, 1, 2, 2, 3, 4], [1, 3, 4, 5])
print(dp_min_ops(10)) # ([0, 1, 2, 2, 3, 4, 3, 4, 4, 3, 4], [1, 3, 9, 10])