转载请标明出处
#include <iostream>
using namespace std;
void test1();
void test2();
class Solution {
public:
int MAX = 0;
//JF9 变态跳台阶
int jumpFloorII(int number);
//JF10 矩形覆盖
int rectCover(int number);
};
int Solution::jumpFloorII(int number) {
//找规律。JF(n)=JF(0)+JF(1)+...+JF(n-i)+...+JF(n-1),其中,i为第一步跳i个台阶
//JF(0)=1,JF(1)=1,JF(2)=2,....。JF(n)的前n-1项恰好为JF(n-1),故JF(n)=JF(n-1)*2
if (number < 0) {
MAX = -1;
return 0;
}
if (number == 0 || number == 1 || number == 2) return number;
int temp = 2 * 2;
int i = 3;
while (i < number) {
temp = 2 * temp;
i++;
}
return temp;
}
int Solution::rectCover(int number) {
//Fibonacci数列。铺满第一个子块(2*1或者2*2),共有f(n-2)+f(n-1)种情况。
//注意,用两个2*1铺,这种情况包含在2*1情况中
if (number < 0) {
MAX = -1;
return 0;
}
if (number == 0 || number == 1 || number == 2) return number;
return(rectCover(number - 1) + rectCover(number - 2));
}
void test1() {
Solution s;
cout << s.jumpFloorII(4);
return;
}
void test2() {
Solution s;
cout << s.rectCover(3);
return;
}
int main() {
//test1();
test2();
system("pause");
return 0;
}