ProgrammingVery hardTCS NQT · Accenture
How many ways are there to climb 20 steps taking 1 or 2 at a time? Compute with this exponential recursion.
How many ways are there to climb 20 steps taking 1 or 2 at a time? Compute with this exponential recursion.
#include <stdio.h>
long long w(int n) {
return n <= 1 ? 1 : w(n - 1) + w(n - 2);
}
int main(void) {
printf("%lld", w(20));
}- A. 6765
- B. 17711
- C. 9999
- D. 10946
Correct answer: D. 10946
w(n) = w(n - 1) + w(n - 2) with w(0) = w(1) = 1 gives Fibonacci numbers, and w(20) = F(21) = 10946. Starting the base case at w(1) = 0 would give 6765.
More C Programming questions
- What is printed? c #include <stdio.h> int calls = 0; int bs(int a, int lo, int hi, int k) { calls++; if (lo > hi) return -1; int m = lo +…
- What is printed? c #include <stdio.h> int main(void) { unsigned x = 0; int c = 0; for (unsigned i = 1; i <= 16; i++) { x = i; while (x) {…
- What is printed? c #include <stdio.h> unsigned alt(unsigned x) { unsigned y = x ^ (x >> 1); return (y & (y + 1)) == 0; } int main(void) {…
- What is printed? c #include <stdio.h> int main(void) { unsigned x = 0xDEADu; printf("%u", (x >> 3) & 0xFu); }
- What is printed? c #include <stdio.h> int main(void) { unsigned a = 0x12345678u, b = 0x12345600u, x = a ^ b; int c = 0; while (x) { c += x…
- What is printed? Assume uint8t is 8 bits. c #include <stdint.h> #include <stdio.h> int main(void) { uint8t acc = 0; uint8t v[] = {200…