题解 | #放苹果#
放苹果
https://www.nowcoder.com/practice/bfd8234bb5e84be0b493656e390bdebf
def count_ways(m, n): # 创建一个二维数组 dp,大小为 (m+1) * (n+1) dp = [[0] * (n + 1) for _ in range(m + 1)] # 初始化边界条件 for j in range(n + 1): dp[0][j] = 1 # 0个苹果,只有1种方法(所有盘子为空) for i in range(1, m + 1): dp[i][0] = 0 # 1个及以上苹果,0个盘子,没有分法 for i in range(1, m + 1): for j in range(1, n + 1): if j > i: dp[i][j] = dp[i][j-1] else: dp[i][j] = dp[i][j-1] + dp[i-j][j] return dp[m][n] # 读取输入 m, n = map(int, input().split()) # 输出结果 print(count_ways(m, n))