题解 | #放苹果#
放苹果
https://www.nowcoder.com/practice/bfd8234bb5e84be0b493656e390bdebf
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextInt()) { // 注意 while 处理多个 case
int a = in.nextInt();
int b = in.nextInt();
System.out.println(getNum(a,b));
}
}
private static int getNum(int a,int b){
int[][] dp = new int[a+1][b+1];
dp[1][1] = 1;//有1个盘子和一个苹果的时候,很明显有1中放法
// i个盘子 0个苹果 1种放法
for(int i=0;i<dp[0].length;i++){
dp[0][i] = 1;
}
//递归的出口有了,那就递归吧,如果i 表示苹果,j表示盘子,那么明显 f(i,j)有空盘子()和无空盘子
for(int i=1;i<dp.length;i++){
for(int j=1;j<dp[1].length;j++){
if(j>i){
dp[i][j] = dp[i][j-1];//有j-i个空盘子取一个盘子对结果没啥影响
}else{
dp[i][j] = dp[i-j][j]+dp[i][j-1];
}
}
}
return dp[a][b];
}
}