UVA10359 - Tiling(递推 + 大数)

UVA10359 - Tiling(递推 + 大数)

题目链接

题目大意:用2 * 1和 2 * 2的瓷砖去铺2 * n的矩形。

解题思路:递推:f(n) = f(n - 1) + 2*f(n - 2) 边界f(0) = 1, f(1) = 1.

代码:

import java.util.*;
import java.math.*;
import java.io.*;

public class Main {

    static BigInteger f[] = new BigInteger[300];

    public static void init() {

        f[0] = f[1] = BigInteger.valueOf(1); 
        for (int i = 2; i <= 250; i++)
            f[i] = f[i - 1].add(BigInteger.valueOf(2).multiply(f[i - 2]));
    }

    public static void main(String args[]) {

        Scanner cin = new Scanner(System.in);
        int n;
        init();

        while (cin.hasNext()) {

            n = cin.nextInt();
            System.out.println(f[n]);
        }
    }
}

你可能感兴趣的:(UVA10359 - Tiling(递推 + 大数))