ProgrammingVery hardGATE CS · Competitive coding (CP)
This recursion counts the tilings of a 2 x n board with 2 x 1 dominoes. What is printed?
This recursion counts the tilings of a 2 x n board with 2 x 1 dominoes. What is printed?
#include <stdio.h>
int ways(int n) {
if (n <= 1) return 1;
return ways(n - 1) + ways(n - 2);
}
int main(void) {
printf("%d", ways(6));
}- A. 13
- B. 8
- C. 21
- D. 14
Correct answer: A. 13
ways(n) follows the Fibonacci numbers starting 1, 1, 2, 3, 5, 8, 13, so ways(6) = 13. Using the wrong base case or reading one index too far gives 8, 14 or 21.
More C Programming questions
- What is printed? c #include <stdio.h> int main(void) { int a[] = {1, 2, 3, 5, 7, 8, 9}; int i = 0, j = 6, c = 0; while (i < j) { int s =…
- What is printed? c #include <stdio.h> int main(void) { unsigned lo = 2000000000u, hi = 2500000000u; printf("%u %u", lo + (hi - lo) / 2…
- What is printed? c #include <stdio.h> struct P { int a; int b[3]; }; int main(void) { struct P arr[2] = {{1, {2, 3, 4}}, {5, {6, 7, 8}}}…
- What is printed? c #include <stdio.h> int main(void) { int c = 0; for (unsigned n = 1; n <= 20; n++) { unsigned x = n; int p = 0; while…
- What is printed? c #include <stdio.h> int main(void) { int c = 0; for (unsigned i = 0; i < 8; i++) { unsigned g = i ^ (i >> 1); unsigned x…
- What is printed? c #include <stdint.h> #include <stdio.h> int main(void) { uint32t a = 65536, b = 65536; uint32t p = a b; printf("%u %s"…