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) 回帖