ProgrammingHardCompetitive coding (CP)
How many ways are there to tile a 1 by 4 strip with 1 by 1 and 1 by 2 tiles? The recursion computes it. What is printed?
How many ways are there to tile a 1 by 4 strip with 1 by 1 and 1 by 2 tiles? The recursion computes it. What is printed?
#include <stdio.h>
int f(int n) {
if (n <= 1) return 1;
return f(n - 1) + f(n - 2);
}
int main(void) {
printf("%d", f(4));
}- A. 4
- B. 5
- C. 8
- D. 3
Correct answer: B. 5
The tiling count follows f(n) = f(n - 1) + f(n - 2) with f(0) = f(1) = 1, so f(2) = 2, f(3) = 3 and f(4) = 5.
More C Programming questions
- What is printed? The program computes the parity of the set bits of 0b1101. c #include <stdio.h> int parity(unsigned x) { return x == 0 ?…
- What is the position of the lowest set bit of 0x0300 (counting bit 0 as position 0)? Compute with this program. c #include <stdio.h> int…
- What is printed? The program inverts the Gray code 6 by XOR-folding its bits. c #include <stdio.h> int main(void) { unsigned g = 6, n = 0…
- What is printed? c #include <stdio.h> int main(void) { unsigned char c = 200; if (c 2 > 300) puts("big"); else puts("small"); }
- What is the difference between the largest and the smallest element of {4, -2, 9, 0}, found in a single pass with pointers? Compute it…
- How many pairs (i, j) with i < j in {1, 3, 5, 8, 10} have a difference of exactly 2 (that is, a[j] - a[i] = 2)? Compute with this program…