ProgrammingVery hardCompetitive coding (CP)
How many ways are there to make 10 from unlimited coins of value 2, 3 and 5? Compute with this program.
How many ways are there to make 10 from unlimited coins of value 2, 3 and 5? Compute with this program.
#include <stdio.h>
int ways(int n, int k) {
if (n == 0) return 1;
if (n < 0 || k == 0) return 0;
int coins[] = {2, 3, 5};
return ways(n - coins[k - 1], k) + ways(n, k - 1);
}
int main(void) {
printf("%d", ways(10, 3));
}- A. 4
- B. 3
- C. 5
- D. 6
Correct answer: A. 4
The recursion counts multisets of coins that total 10: 2+2+2+2+2, 2+3+5, 5+5, and 2+2+3+3. These are the four ways.
More C Programming questions
- What is the length of the longest common subsequence of ABCBDAB and BDCABA? The program computes it by plain recursion without a table…
- How many islands does this 4-connected flood-fill count find in the 3 by 3 grid "101", "010", "101"? Compute with the program. c #include…
- How many of these statements are true when x = 255? Count them with this program. c #include <stdint.h> #include <stdio.h> int main(void)…
- What is printed? c #include <stdio.h> int main(void) { unsigned a = 3; int b = -5; printf("%d", (a + b) > 0); }
- What is printed? c #include <stdio.h> #include <string.h> int main(void) { sizet n = 0; printf("%d", n - 1 > 100); }
- What is printed? c #include <stdio.h> int pal(const char s, int n) { int l = 0, r = n - 1; while (l < r) { if (s[l] != s[r]) return 0…