首页 > 360后台软件开发笔试
头像
如此艰难
编辑于 2020-08-16 16:58
+ 关注

360后台软件开发笔试

n元发工资,每次可发1-n元,求方案数。我的做法有哪里不对吗? 4元8种,5元16种。。
def CalulateMethodCount(num_money):
    # write code here
    def f(path, rest, num_money):
        # nonlocal solution
        if sum(path) == num_money:
            solution.add(tuple(path))
            print(path)
            return
        for i in range(1, rest+1):
            path.append(i)
            f(path, rest-i, num_money)
            path.pop()
    solution = set()
    path = []
    f(path, num_money, num_money)
    return len(solution)

print(CalulateMethodCount(5))
[1, 1, 1, 1, 1]
[1, 1, 1, 2]
[1, 1, 2, 1]
[1, 1, 3]
[1, 2, 1, 1]
[1, 2, 2]
[1, 3, 1]
[1, 4]
[2, 1, 1, 1]
[2, 1, 2]
[2, 2, 1]
[2, 3]
[3, 1, 1]
[3, 2]
[4, 1]
[5]
16




全部评论

(1) 回帖
加载中...
话题 回帖

相关热帖

近期热帖

历年真题 真题热练榜 24小时
技术(软件)/信息技术类
查看全部

近期精华帖

热门推荐