新手第一次用Python写这种难度的算法,有些吃力,以后就好了:)
- import math
- def func(my_list, used_baskets, eggs, baskets, max_eggs_per_basket):
- if eggs == 0:
- for i in range(used_baskets-1):
- print(my_list[i], ' ', sep='', end='')
- print(my_list[used_baskets-1])
- elif eggs == 1 or baskets == 1:
- for elt in my_list[0:used_baskets]:
- print(elt, ' ', sep='', end='')
- print(eggs)
- else:
- for x in range(min(eggs, max_eggs_per_basket), math.ceil(eggs/baskets)-1, -1):
- my_list[used_baskets] = x
- func(my_list, used_baskets+1, eggs-x, baskets-1, x)
- def main():
- m = int(input("input your eggs:"))
- if m < 1 or m > 100:
- quit()
- n = int(input("input your baskets:"))
- if n < 1 or n > 100:
- quit()
- input_list = [0]*n
- func(input_list, 0, m, n, m)
- main()
阅读(3816) | 评论(0) | 转发(0) |