找到最好的解决方法使剩的空间最少,但是我感觉递归的没问题,就是结果不是很对# recursive backtracking algorithm to solve CD problem
someSongs = [("a large song", 60), ("a little song", 10), ("a bigger song", 20), ("a very short song", 2),
("a tiny song", 1)]
def CDSolve(songsToUse, space):
# returns a list of accommodated songs as (title,length tuples) and remaining space
list = []
# # base cases
if len(songsToUse) == 0:
return list, space
# # if there are no more songs to consider:
# # return solution
if space == 0:
return list, space
# # if there is no more room on the CD:
# # return solution
不知道你这个问题是否已经解决, 如果还没有解决的话: