ProgrammingVery hardCompetitive coding (CP)
What is printed?
What is printed?
#include <stdio.h>
long long memo[50];
int calls = 0;
long long f(int n) {
calls++;
if (n < 2) return 1;
if (memo[n]) return memo[n];
return memo[n] = f(n - 1) + f(n - 2);
}
int main(void) {
long long r = f(10);
printf("%lld %d", r, calls);
}- A. 89 21
- B. 89 20
- C. 89 19
- D. 55 19
Correct answer: C. 89 19
The first call to f(10) computes f(9) and so on down to f(2), each memoised, so the second recursive call from f(10) returns from the memo at once. Counting calls by hand gives 19 in total.
More C Programming questions
- What is printed? c #include <stdio.h> #include <stdlib.h> int cmp(const void a, const void b) { int x = (const int )a, y = (const int )b…
- What is printed? c #include <stdint.h> #include <stdio.h> int main(void) { uint16t x = 0xABCD; x = (uint16t)((x << 4) | (x >> 12))…
- Assume unsigned int is 32 bits. What is printed? c #include <stdio.h> int main(void) { unsigned n = 0x1FFFF, p = 1; while (p 2 <= n) p =…
- Assume uint32t is 32 bits. What is printed? c #include <stdint.h> #include <stdio.h> int main(void) { uint32t v = 0x12345678u; uint32t r =…
- What is printed? c #include <stdint.h> #include <stdio.h> int main(void) { uint8t x = 250; int n = 0; do { x += 3; n++; } while (x != 4)…
- Assume unsigned int is 32 bits. What is printed? c #include <stdio.h> int main(void) { int x = -17; unsigned y = (unsigned)x; printf("%u"…