题解 | #放苹果#
放苹果
https://www.nowcoder.com/practice/bfd8234bb5e84be0b493656e390bdebf
import sys #思路来源:https://blog.csdn.net/m0_45816418/article/details/109656592 def wayNum_setapple(apple_num,dish_num): #这个题目显然是要使用递归的方案的 #输入判断 if apple_num < 0 or dish_num < 1: print("输出有误,请注意苹果数和盘子数的范围。") return 0 #递归出口 if apple_num == 0 or dish_num == 1: return 1 #递归过程 #思路简介:盘子太多,则结果和没有这些空盘子一样 #至少有一个空盘子,则wayNum_setapple(apple_num, dish_num-1) #没有空盘,至少有一个,那和苹果一盘少一个的分法是一样的:wayNum_setapple(apple_num-dish_num, dish_num) if apple_num < dish_num: return wayNum_setapple(apple_num, apple_num) else: return wayNum_setapple(apple_num, dish_num-1)+wayNum_setapple(apple_num-dish_num, dish_num) apple_dish_num = input().split(' ') apple_num ,dish_num = int(apple_dish_num[0]),int(apple_dish_num[1]) print(wayNum_setapple(apple_num,dish_num))