|
发表于 2020-8-9 13:53:07
|
显示全部楼层
- # -*- coding:utf-8 -*-
- # author: HPCM
- # time: 2020/8/8 21:39
- # file: test.py
- from functools import lru_cache
- # nums = 3
- nums = int(input("请输入拆分的整数: "))
- choice = [1, 2, 4, 8, 16, 32, 64, 128]
- choice.sort()
- @lru_cache()
- def foo(num, i=-1):
- output_list = []
- use = choice[i]
- y = num - use
- if y < 0:
- output_list += foo(num, i - 1)
- elif y == 0:
- output_list.append(str(use))
- else:
- output_list.append(str(use))
- output_list += (foo(y, i))
- return output_list
- print("{} = {}".format(nums, " + ".join(foo(nums))))
复制代码 |
|