ProgrammingHardCompetitive coding (CP)

What is printed?

What is printed?
#include <stdio.h>
int ways(int n, int coins[], int k) {
    if (n == 0) return 1;
    if (n < 0 || k == 0) return 0;
    return ways(n - coins[k - 1], coins, k) + ways(n, coins, k - 1);
}
int main(void) {
    int coins[] = {1, 2, 5};
    printf("%d", ways(10, coins, 3));
}
  1. A. 10
  2. B. 128
  3. C. 6
  4. D. 0

Correct answer: A. 10

Splitting on 'use coin k again or drop it for good' counts multisets, so order does not matter: 10 ways to make 10 from {1, 2, 5}. 128 counts ordered sequences, 6 forgets the 5 coin, and 0 assumes each coin can be used only once.

More C Programming questions

Exams that ask this topic