m0_46561774 2020-03-20 19:04 采纳率: 0%
浏览 142

python递归循环之刻录歌曲

找到最好的解决方法使剩的空间最少,但是我感觉递归的没问题,就是结果不是很对# recursive backtracking algorithm to solve CD problem

inputs: 1. a list of songs given as (title,length) tuples

2. a (maybe partly-filled) CD, represented by an integer measuring remaining space

output: 1. list of song titles accommodated on CD, minimising the amount of remaining space

2. integer representing remaining space

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



  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2022-09-09 19:38
    关注
    不知道你这个问题是否已经解决, 如果还没有解决的话:

    如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 以帮助更多的人 ^-^
    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘