POJ 1664 放苹果
2015-12-01 12:17
281 查看
很有趣的递推题目。通过递推不断缩小题目规模。
base case: 没有苹果或者只剩一个碟子,此时只有一种方法。
如果碟子比苹果多的话,那么把多出来的碟子去掉结果是一样的(它们只能空着)
否则的话,分为所有碟子都有苹果(此时将苹果总数减去碟子数,即每一个碟子都有一个苹果) + 有一个碟子没有苹果(碟子数减一)
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)); } } }
相关文章推荐
- ZOJ - 2112 Dynamic Rankings(BIT套主席树)
- PhpStorm WebMatrix xDebug 配置开发环境
- 权限之路径拦截
- hough变换是如何检测出直线和圆的?
- FastReport经验
- Oracle修改表结构字段名和字段长度
- Oracle单组函数
- PHP接收json 并将接收数据插入数据库的实现代码
- linux下软链接和硬链接的区别 【转】
- 可扩展机器学习——概述
- leetcode ugly number 2
- android 获得屏幕状态
- 单位和尺寸
- linux下VI模式中上下左右键和回退键出现字母
- hdu3949
- 【非技术】谈谈简历那些事儿
- AppleWatch___学习笔记(三)iPhone和Apple Watch上的数据同步
- linux开机到登陆的启动过程描述 【转】
- android-Storage Access Framework
- poj 3185