ProgrammingVery hardCompetitive coding (CP)

How many ordered ways are there to write 10 as a sum of 1s and 4s? Compute with this program.

How many ordered ways are there to write 10 as a sum of 1s and 4s? Compute with this program.
#include <stdio.h>
int f(int n) {
    if (n == 0) return 1;
    if (n < 0) return 0;
    return f(n - 1) + f(n - 4);
}
int main(void) {
    printf("%d", f(10));
}
  1. A. 12
  2. B. 14
  3. C. 11
  4. D. 16

Correct answer: B. 14

The recursion splits by the last part, which is 1 or 4, so f(n) = f(n - 1) + f(n - 4). The values for n = 0 to 10 are 1, 1, 1, 1, 2, 3, 4, 5, 7, 10, 14.

More C Programming questions

Exams that ask this topic