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);
}
  1. A. 89 21
  2. B. 89 20
  3. C. 89 19
  4. 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

Exams that ask this topic