小明现在手里有 m 个完全相同的1元硬币,同时小明有 n 个完全一样的存钱罐。
现在小明想把硬币放到存钱罐中,问有多少种不同的放法?(允许有空的存钱罐)
比如:3个硬币,3个存钱罐(1, 1, 1)(2, 1, 0)(3, 0, 0)一共3种情况
m个苹果,n个盘子
解题思路就是从两种情况:
一种是有盘子是空的即分(m,n-1),一种是没有盘子是空的即每个盘子至少一个苹果那就是相当于又分(m-n,n),出口就是要不是n个苹果分1个盘子一种分法,要不就是1个苹果分m个盘子一种分法,另外的就是苹果数小于盘子数那就是相当于n个苹果分n个盘子
#include<iostream>
using namespace std;
int apple(int m, int n)
{
if (m <= 1 || n == 1)
return 1;
else
{
if (m < n)
return apple(m, m);
else
return apple(m, n - 1) + apple(m - n, n);
}
}
int main()
{
int a, b;
cin >> a >> b;
cout << apple(a, b) << endl;
return 0;
system("pause");
return 0;
}