|
如果只是 6元、13元、15元、20元,零用錢 35元整
代碼:
- from itertools import permutati**
- toys = [6, 13, 15, 20]
- buy = [[list(price) for price in permutati**(toys, i) if sum(price) == 35] for i in range(len(toys))]
- res = []
- for i in buy:
- if i:
- for j in i:
- if sorted(j) not in res:
- res.append(j[:])
- print(*res)
复制代码如果涉及其他輸入可能性,就不能用這代碼,因為考慮到購買某樣玩具可能不止一次,又或者玩具種類多至上百種,零錢極大數值等問題
|
|