对于m个苹果,n个盘子f(m,n):
如果m<n,那么就跟m个盘子,m个苹果是一样的f(m,m)。
如果m>n,那么有两种情况:一种有空盘子的情况,一种没有空盘子的情况,两种情况不重叠且加一起一定为情况总数。
第一种情况:m个苹果放在n-1个盘子里,因为至少有1个空盘子,即f(m,n-1)
第二种情况:每个盘子都至少有一个苹果,m-n个苹果再放到n个盘子里,即f(m-n,n)。
由上面两种情况得到递归式f(m,n)=f(m,n-1)+f(m-n,n)。
#include <iostream>
using namespace std;
int count(int a,int b)
{
if(a==0||b==1)
return 1;
if(a<b)
return count(a,a);
else
{
return count(a-b,b)+count(a,b-1);
}
}
int main()
{
int a,b;
cin>>a>>b;
cout<<count(a,b)<<endl;
return 0;
}