题解 | #放苹果#
放苹果
https://www.nowcoder.com/practice/bfd8234bb5e84be0b493656e390bdebf
动态规划解法
#华为机试#public static void main(String[] args) {Scanner sc = new Scanner(System.in); int apple = sc.nextInt(); int plate = sc.nextInt(); // 初始化首行数值,也就是只有一个盘子情况 int[] preRow = new int[apple + 1]; Arrays.fill(preRow, 1); // 从两个盘子开始循环填充 for (int j = 1; j < plate; j++) { int[] currRow = new int[apple + 1]; for (int i = 0; i < apple + 1; i++) { // j+1是为了解决盘子存在数组下标为0开始实际差了1的问题 if (i < j + 1) { currRow[i] = preRow[i]; } else { currRow[i] = currRow[i - (j + 1)] + preRow[i]; } } preRow = currRow; } System.out.println(preRow[apple]); }