Title Description
We can use 2 small rectangle 1 of sideways or vertically to cover a larger rectangle. Will the use of n 2 small rectangle 1 coverage without overlap a large rectangle 2 * n, a total of how many ways?
A solution to a problem: Recursive
1 // similar Fibonacci recursion 2 public static int RectCover ( int target) { . 3 IF (target <= 0 ) { . 4 return 0 ; . 5 } the else IF (target == 1 == target || 2 ) { . 6 return target; . 7 } the else { . 8 return RectCover (-target. 1) + RectCover (target-2 ); . 9 } 10 }
1 public static int RectCover01(int target) { 2 if(target <= 2) { 3 return target; 4 } 5 int one = 1; 6 int two = 2; 7 int result = 0; 8 for (int i = 3; i <= target; i++) { 9 result = one + two; 10 one = two; 11 two = result; 12 } 13 return result; 14 }
test:
1 public static void main(String[] args) { 2 Scanner scanner = new Scanner(System.in); 3 while (scanner.hasNext()){ 4 int anInt = scanner.nextInt(); 5 int cover = RectCover(anInt); 6 System.out.println(cover); 7 } 8 } 9 输入:0 1 2 3 4 5 6 7 8 9 10 10 输出:0 1 2 3 5 8 13 21 34 55 89