ProgrammingVery hardCompetitive coding (CP)

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

How many ordered ways are there to write 10 as a sum of 1s, 2s and 3s? 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(10));
}
  1. A. 256
  2. B. 181
  3. C. 200
  4. D. 274

Correct answer: D. 274

Each ordered sum is counted once by its last part, which is 1, 2 or 3, so f(n) = f(n-1) + f(n-2) + f(n-3). The sequence gives f(10) = 274.

More C Programming questions

Exams that ask this topic