很有趣的递推题目。通过递推不断缩小题目规模。
base case: 没有苹果或者只剩一个碟子,此时只有一种方法。
如果碟子比苹果多的话,那么把多出来的碟子去掉结果是一样的(它们只能空着)
否则的话,分为所有碟子都有苹果(此时将苹果总数减去碟子数,即每一个碟子都有一个苹果) + 有一个碟子没有苹果(碟子数减一)
#include <iostream>
#include <cstdio>
using namespace std;
int count(int m, int n) // m apples, n dishes
{
if (m == 0 || n == 1)
return 1;
if (n > m)
return count(m, m);
return count(m, n - 1) + count(m - n, n);
}
int main()
{
int cnt;
int apple, dish;
while (scanf("%d", &cnt) != EOF)
{
while (cnt--)
{
scanf("%d%d", &apple, &dish);
printf("%d\n", count(apple, dish));
}
}
}