采用递归的思想将此事件无限细分,每个事件可以分为f(m,n)=f(m-n,n)+f(m,n-1);f(m-n,n)是当苹果数大于等于盘子数的情况,f(m,n-1)是当苹果数小于盘子数的情况。当此事件分到苹果数为0或苹果数为1或盘子数为1的时候返回1,当苹果数小于0或盘子数小于等于0返回0.
import java.util.Scanner;
public class Main{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
while(sc.hasNextInt())
{
System.out.println(count(sc.nextInt(),sc.nextInt()));
}
sc.close();
}
public static int count(int m,int n)
{
if(m<0||n<=0)
return 0;
//细分到苹果数为一或盘子数为一的情况返回一
if(m==1||n==1||m==0)
return 1;
//将此事件无线细分
return count(m,n-1)+count(m-n,n);
}
}
京公网安备 11010502036488号