ProgrammingVery hardCompetitive coding (CP)

How many ordered ways are there to write 6 as a sum of parts 1, 2 and 3? Compute with this program.

How many ordered ways are there to write 6 as a sum of parts 1, 2 and 3? 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 - 2) + f(n - 3);
}
int main(void) {
    printf("%d", f(6));
}
  1. A. 21
  2. B. 24
  3. C. 26
  4. D. 32

Correct answer: B. 24

f(n) = f(n - 1) + f(n - 2) + f(n - 3) with f(0) = 1 counts ordered sums by their last part, and it gives 1, 1, 2, 4, 7, 13, 24 for n = 0 to 6.

More C Programming questions

Exams that ask this topic