题解 | #放苹果#
放苹果
http://www.nowcoder.com/practice/bfd8234bb5e84be0b493656e390bdebf
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
//apple个数
int appleNum = in.nextInt();
//盘子个数
int plateNum = in.nextInt();
//计算分法
System.out.print(devideWays(appleNum, plateNum));
}
//递归方法
// public static int devideWays(int appleNum, int plateNum){
// if(appleNum < 0 || plateNum <= 0){
// return 0;
// }
// if(appleNum == 0 || appleNum == 1 || plateNum == 1){
// return 1;
// }
// else{
// return devideWays(appleNum, plateNum - 1) + devideWays(appleNum - plateNum, plateNum);
// }
// }
//动态规划
public static int devideWays(int appleNum, int plateNum){
int len1 = appleNum;
int len2 = plateNum;
int[][] dp = new int[len1 + 1][len2 + 1];
//一个盘子或没有盘子的时候
for(int i = 0; i <= len1; i++){
dp[i][1] = 1;
}
//一个苹果或者凌哥果
for(int j = 0; j <= len2; j++){
dp[0][j] = 1;
dp[1][j] = 1;
}
for(int i = 2; i <= len1; i++){
for(int j = 1; j <= len2; j++){
dp[i][j] = dp[i][j - 1] + (i >= j ? dp[i - j][j] : 0);
}
}
return dp[len1][len2];
}
}