Week 8: Exercises

1. Coin Sums

Write a function that takes an integer N and returns the number of possible ways to form a sum of N crowns using Czech coins (which have denominations 1 Kč, 2 Kč, 5 Kč, 10 Kč, 20 Kč, 50 Kč). Order matters: 1 + 5 and 5 + 1 are distinct sums. Use dynamic programming for an efficient solution.

2. Carrots and Parsley

A garden contains N plant beds, all in a row. Each bed will contain either carrots or parsley, but no two neighboring beds may both contain parsley.

a) Write a function void garden(int n) that takes N, the size of the garden, and prints out all possible ways that exist to plant these vegetables in the garden. For example, garden(5) might print

C C C
P C C
C P C
C C P
P C P

b) Write a function int ways(int n) that takes N, the size of the garden, and returns the number of possible ways that exist to plant these vegetables in the garden. For example, ways(5) will print 5.

3. Smallest Number of Coins

a) Write a function that takes an array coins[] with the denominations of various coins, plus an integer sum. The method should return the smallest number of coins that can form the given sum. Each denomination may be used any number of times in the sum. For example, if coins = { 1, 3, 4 } and sum is 6, the function will return 2 since there are 2 coins in the sum 3 + 3.

b) Modify your function so that it returns an array with the actual coin values needed to form the sum minimally.

4. Minimum Number of Jumps

Consider this problem: we are given a horizontal array of squares, each with an integer. For example the array might be

2 3 1 1 2

A button begins on the leftmost square. At each step, if the button is on a square with the number k then the button may jump to the right by any number of squares that is less than or equal to k. For example, with the array above, in the first step the button may jump right either by 1 or 2 squares. We wish to determine the smallest number of steps needed for the button to jump past the right edge of the array. For example, with the array above, this number is 3, since the button can jump right by 1, then by 3, then by 1 or 2.

a) Write a function jumps() that takes an array of integers, and returns the smallest number of steps needed to jump past the right edge.

b) Modify your function so that it also returns a string containing the sizes of the jumps to take to achieve this minimum.