标签:

2 8 12 -1
3 153 2131
如果长度L是2,那么有3种拼法,所以是3*f[n-2] 如果长度L是4或以上,有2种拼法,所以是2*f[n-L] (L 从4到n)
前面我们最初拼的时候只取了对称的两种情况之一,因此,如果x>2,
拼出一个不能被竖线切割的矩形的方法只有两种。那么递推公式自然就有了,f(n)=3*f(n-2)+2*f(n-4)+…+2*f(0),
然后再写出f(n-2)的递推式后两式作差就可以得到f(n)=4*f(n-2)-f(n-4),
递归的边界是f(0)=1,f(2)=4。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[] mat = new int[31];
mat[0] = 1;//初始为1***
mat[2] = 3;
for (int i = 4; i < mat.length; i+=2) {
mat[i] = mat[i-2]*4 - mat[i-4];
}
while (sc.hasNext()) {
int n = sc.nextInt();
if (n == -1) {
return;
}
if ((n & 0x01) == 0) {// 偶数
System.out.println(mat[n]);
}else{
System.out.println(0);
}
}
}
}
标签:
原文地址:http://blog.csdn.net/hncu1306602liuqiang/article/details/46389483