矩形覆盖
2017-07-09 18:30
239 查看
我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形。
请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法?
请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法?
本题实质是斐波拉契数列,类似问题——跳台阶、斐波拉契数列
/** * 解法一——递归解法 * */ public class Solution { public static void main(String[] args) { Solution solution = new Solution(); System.out.print("请输入n的值:"); @SuppressWarnings("resource") Scanner in = new Scanner(System.in); int n = in.nextInt(); System.out.println("组合方法:" + solution.RectCover(n)); } public int RectCover(int target) { return (target <= 2) ? target : RectCover(target - 1) + RectCover(target - 2); } }
/** * 解法一——一般循环解法 * */ public class Solution { public static void main(String[] args) { Solution solution = new Solution(); System.out.print("请输入n的值:"); @SuppressWarnings("resource") Scanner in = new Scanner(System.in); int n = in.nextInt(); System.out.println("组合方法:" + solution.RectCover(n)); } public int RectCover(int target) { int a = 1; int b = 2; int result = 0; if(target == 1) { result = 1; } else if(target == 2) { result =2; } else { for(int i = 0; i < target - 2; i++) { result = a + b; a = b; b = result; } } return result; } }
相关文章推荐
- 剑指offer--矩形覆盖
- 在SurfaceView中实现覆盖刷新和脏矩形刷的方法
- 2015百度之星初赛(一) 矩形面积 1006【最小矩形覆盖】
- 矩形覆盖
- 跳台阶,变态跳台阶,矩形覆盖 代码实现
- 【练习笔记】剑指offer-矩形覆盖
- 矩形覆盖
- 剑指Offer之矩形覆盖
- 矩形覆盖
- 剑指offer-矩形覆盖
- 剑指offer 矩形覆盖 @python
- 扫描线 求n个矩形覆盖后的总面积之和 hdu1542 Atlantis
- [Jobdu] 题目1390:矩形覆盖
- 多少个矩形被覆盖
- Android SurfaceView 绘图覆盖刷新及脏矩形刷新方法
- 剑指offer 10 矩形覆盖
- 剑指offer 面试题9:斐波那契数列及其变形(跳台阶、矩形覆盖) 题解
- 剑指offer之面试题9-4:矩形覆盖
- 斐波那契数列及其变形问题(跳台阶,变态跳台阶,矩形覆盖)
- 剑指offer(10):矩形覆盖问题