Shirlies
宁静专注认真的程序媛~

题目

我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形。请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法?

链接

http://www.nowcoder.com/practice/72a5a919508a4251859fb2cfb987a0e6?tpId=13&tqId=11163&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking

答案

一个:1

二个:2

三个:1 + 2

...

N个:f(n-1) + f(n-2)

代码

 1 class Solution {
 2 public:
 3     int rectCover(int number) {
 4         if(number == 0){
 5             return 0;
 6         }
 7         if(number == 1){
 8             return 1;
 9         }
10         
11         if(number == 2){
12             return 2;
13         }
14         
15         int next;
16         int first = 1;
17         int second = 2;
18         for(int index = 3; index <= number; ++ index){
19             next = first + second;
20             first = second;
21             second = next;
22         }
23         
24         return next;
25     }
26 };
View Code

 

posted on 2016-08-16 16:59  Shirlies  阅读(171)  评论(0)    收藏  举报