题解 | #放苹果#
放苹果
http://www.nowcoder.com/practice/bfd8234bb5e84be0b493656e390bdebf
//递归思路其它答案已经讲得很清晰了,自己在纸上画一画就明白了。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){//支持多行输入
Scanner scanLine = new Scanner(sc.nextLine());
int m = scanLine.nextInt();
int n = scanLine.nextInt();
System.out.println(distributeApple(m, n));
scanLine.close();
}
sc.close();
}
public static int distributeApple(int m, int n){
if(m<0 || n<=0){//苹果可以为0,盘子至少有1个。
return 0;
}
else if(m==0 || n==1 || m == 1){//一直递归到苹果为1或0,或盘子为1。
return 1;
}
else{
return distributeApple(m,n-1)+distributeApple(m-n,n);//递归
}
}
}