放苹果
放苹果
http://www.nowcoder.com/questionTerminal/bfd8234bb5e84be0b493656e390bdebf
import java.io.*;
import java.util.*;
public class Main{
public static void main(String[] args) throws Exception{
Scanner sc = new Scanner(System.in);
while(sc.hasNextInt()){
int a = sc.nextInt();
int b = sc.nextInt();
System.out.println(count(a, b));
}
}
/**
* m 苹果数
* n 盘子数
*/
public static int count(int m, int n){
// 一个盘 或者 没有苹果 代表一种方案
if(n == 1 ||m == 0 ) return 1;
// 盘子过多情况,多余的盘子不起任何作用,最大的有效盘子是 m 个
else if(n > m) return count(m, m);
// 情况一: 只用 b - 1个盘子
// 情况二: 每个盘子里先放一个苹果,等价于 a - b个苹果放到 b 个盘子
else return count(m, n - 1) + count(m - n, n);
}
}
查看18道真题和解析