您的位置:首页 > 其它

POJ 1664 放苹果

2015-12-01 12:17 281 查看
很有趣的递推题目。通过递推不断缩小题目规模。

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));
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: