http://poj.org/problem?id=2663
题目
Description
In how many ways can you tile a 3xn rectangle with 2x1 dominoes?
Here is a sample tiling of a 3x12 rectangle.
Input
Input consists of several test cases followed by a line containing -1. Each test case is a line containing an integer 0 <= n <= 30.
Output
For each test case, output one integer number giving the number of possible tilings.
Sample Input
2
8
12
-1
Sample Output
3
153
2131
思路
推导出公式,因为输入n不大,可以提前求出所有结果保存下来。 手写的推导过程,写的不太整齐-_-||。注意本题n=0时需要输出1。
代码
#include <iostream>
int main() {
const int MAX_COUNT = 31;
int result[MAX_COUNT];
memset(result, 0, sizeof(result));
result[0] = 1;
int cum = 0;
for (int i = 2; i < MAX_COUNT; i += 2) {
result[i] = 3 * result[i - 2];
result[i] += cum * 2;
cum += result[i - 2];
//printf("%d\n", result[i]);
}
int n = 0;
while (scanf("%d", &n) != EOF && n != -1) {
printf("%d\n", result[n]);
}
return 0;
}