题解 | #放苹果#
放苹果
https://www.nowcoder.com/practice/bfd8234bb5e84be0b493656e390bdebf
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int m = sc.nextInt(); int n = sc.nextInt(); System.out.println(getCount(m, n)); } } private static int getCount(int m, int n) { // dp数组表示有N个盘子时候,一共多少种方法 int[][] dp = new int[m + 1][n + 1]; for (int i = 0; i <= n; i++) { dp[0][i] = 1; } for (int i = 0; i <= m; i++) { dp[i][1] = 1; } for (int i = 1; i <= m; i++) { for (int j = 2; j <= n; j++) { // 苹果比盘子多 if (i >= j) { dp[i][j] = dp[i][j - 1] + dp[i - j][j]; } else { // 盘子比苹果多,那么少一个盘子也是相同的结果 dp[i][j] = dp[i][j - 1]; } } } return dp[m][n]; } }