题解 | #放苹果#
放苹果
https://www.nowcoder.com/practice/bfd8234bb5e84be0b493656e390bdebf
import java.util.Scanner; // 动态规划表:n为行,m为列 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); // 注意 hasNext 和 hasNextLine 的区别 while (in.hasNext()) { // 注意 while 处理多个 case int m = in.nextInt(); int n = in.nextInt(); if (n < 2 || m < 2) { System.out.println(1);//当只有一个盘子或者一个苹果时,结果只有一种。 } else { int[][] t = new int[n + 1][m + 1]; for (int r = 0; r <= m; r++) { t[0][r] = 0; } for (int r = 0; r <= m; r++) { t[1][r] = 1; } for (int r = 2; r <= m; r++) { t[2][r] = r / 2 + 1;//盘子数为2时,结果有规律,直接填 } for (int c = 1; c <= n; c++) { t[c][0] = 1; } for (int c = 1; c <= n; c++) { t[c][1] = 1; } //动态规划表公式:分苹果数小于盘子数时、苹果数大于等于盘子数时两种情况 for (int i = 3; i <= n; i++) { for (int j = 2; j <= m; j++) { if (j < i) { t[i][j] = t[i - 1][j]; } else if (j >= i) { t[i][j] = t[i - 1][j] + t[i][j - i]; } } } System.out.println(t[n][m]); } } } }