Question

In: Computer Science

Suppose that the denomination of the coins in a country are c1 > c2 > ....

Suppose that the denomination of the coins in a country are c1 > c2 > . . . cn (e.g. 25, 10, 5, 1 for the United States). The problem to consider is: Given an integer a, find the minimum number of coins needed to make a-cents change. (We assume c1 = 1 and we have unlimited supply of each of the coin types, so that it is always possible to make change for any amount a.) For example, let c3 = 4, c2 = 2, c1 = 1 and a = 6. Then there are 6 distinct ways to make 6-cents change: (6 = 6 × 1, 6 = 4 × 1 + 2, 6 = 1 + 1 + 2 + 2, 6 = 2 + 2 + 2, 6 = 1 + 1 + 4, 6 = 2 + 4). So the answer for this problem instance is 2 coins. (a) A simple heuristic strategy for solving this problem is the following: Always pick the coins with the largest value. For example, for US coins, c1 = 1, c2 = 5, c3 = 10, c4 = 25 and a = 63. We will pick: two 25 coins, one 10 coin, three 1 coin. So we use a total of 6 coins. This strategy does not always work. Describe a counter example. Note: this strategy works for US coins (although the proof is not trivial). So your counter example must use a different coin denominations. (b) Describe a dynamic programming formulation for finding the minimum number of coins needed to make the change, as follows: Define an array A[0..n; 0..a]. The entry A[i, j] is the minimum number of coins needed to make j-cents change, using only the coins with the values c1, c2, . . . , ci . Derive a recursive formula for the entry A[i, j]. The entry A[n, a] will be the answer to the problem. (c) Describe a dynamic programming algorithm for calculating A[∗, ∗] in proper order. The runtime of the algorithm should be at most O(na). (d) Describe an algorithm, that uses A[∗, ∗] and any additional information, to output the optimal solution of the problem. (Namely, output the actual coins needed to make the a cents change).

Solutions

Expert Solution

public class coin {

   static long getNumberOfWays(long N, long[] Coins) {
       // Create the ways array to 1 plus the amount to stop overflow
       long[] ways = new long[(int) N + 1];

       // Set the first way to 1 because its 0 and there is 1 way to make 0
       // with 0 coins
       ways[0] = 1;

       // Go through all of the coins
       for (int i = 0; i < Coins.length; i++) {

           // Make a comparison to each index value of ways with the coin
           // value.
           for (int j = 0; j < ways.length; j++) {
               if (Coins[i] <= j) {

                   // Update the ways array
                   ways[j] += ways[(int) (j - Coins[i])];
               }
           }
       }

       // return the value at the Nth position of the ways array.
       return ways[(int) N];
   }

   static void printArray(long[] coins) {
       for (long i : coins)
           System.out.println(i);
   }

   public static void main(String args[]) {
       long Coins[] = { 1, 5, 10, 25 };

       System.out.println("The Coins are:");
       printArray(Coins);

       System.out.println("Distinct Ways:");
       System.out.println(getNumberOfWays(63, Coins));
   }
}


Related Solutions

Calculate the values "c1, c2, c3, c4, c5" with superposition 6(c1) – (c3)=50 -3(c1) + 3(c2)=0...
Calculate the values "c1, c2, c3, c4, c5" with superposition 6(c1) – (c3)=50 -3(c1) + 3(c2)=0 9(c3) – (c2)= 160 -(c2) – 8(c3) – 2(c5) + 11(c4)=60 -3(c1) – (c2) + 4(c5)=10
Two separate capacitors, C1 and C2 C1 = 36 micro-Coulomb on 3 micro-Farad C2 = 72...
Two separate capacitors, C1 and C2 C1 = 36 micro-Coulomb on 3 micro-Farad C2 = 72 uC on X 2uF, , if zero 1 C2 had a gap of 0.2m maintained by a compressed plastic spring inside the gap, the natural spring length was 0.5m, the compressed spring length was 0.2 m. Spring constant = 8,000 micro-Newton/ meter Action: Connected the two capacitors in parallel Part A Find Q2-new, C2-new, new gap, Hint: Capacitance has geometry parameters, build an equation...
Two separate capacitors, C1 and C2 C1 = 36 micro-Coulomb on 3 micro-Farad C2 = 72...
Two separate capacitors, C1 and C2 C1 = 36 micro-Coulomb on 3 micro-Farad C2 = 72 uC on 5 uF C2 had a gap of 0.2m maintained by a compressed plastic spring inside the gap, the natural spring length was 0.5m, the compressed spring length was 0.2 m. Spring constant = 8,000 micro-Newton/ meter Action: Connected the two capacitors in parallel Part A Find Q2-new, C2-new, new gap, Part B Find the initial total energy, the final total energy -use...
Consider the following 2-period model U(C1,C2) = min{3C1,4C2} C1 + S = Y1 – T1 C2...
Consider the following 2-period model U(C1,C2) = min{3C1,4C2} C1 + S = Y1 – T1 C2 = Y2 – T2 + (1+r)S Where C1 : first period consumption C2 : second period consumption S : first period saving Y1 = 20 : first period income T1 = 5 : first period lump-sum tax Y2 = 50 : second period income T2 = 10 : second period lump-sum tax r = 0.05 : real interest rate Find the optimal saving, S*
please show how to solve Harvey Habit has a utility function U(c1, c2) = min{c1, c2},...
please show how to solve Harvey Habit has a utility function U(c1, c2) = min{c1, c2}, where c1 and c2 are his consumption in periods 1 and 2 respectively. Harvey earns $189 in period 1 and he will earn $63 in period 2. Harvey can borrow or lend at an interest rate of 10%. There is no inflation. a. Harvey will save $60. b. Harvey will borrow $60. c. Harvey will neither borrow nor lend. d. Harvey will save $124....
U(C1, C2, C3, C4, C5) = C1∙C2∙C3∙C4∙C5 As a mathematical function, does U have a maximum...
U(C1, C2, C3, C4, C5) = C1∙C2∙C3∙C4∙C5 As a mathematical function, does U have a maximum or minimum value? What values of Ci correspond to the minimum value of U? What values of Ci correspond to the maximum value of U? Do these values of Ci make sense from an economic standpoint? Now let us connect the idea of economic utility to actual dollar values. To keep the values more manageable, we will use household income rather than the entire...
U(C1, C2, C3, C4, C5) = C1∙C2∙C3∙C4∙C5 As a mathematical function, does U have a maximum...
U(C1, C2, C3, C4, C5) = C1∙C2∙C3∙C4∙C5 As a mathematical function, does U have a maximum or minimum value? What values of Ci correspond to the minimum value of U? What values of Ci correspond to the maximum value of U? Do these values of Ci make sense from an economic standpoint? Now let us connect the idea of economic utility to actual dollar values. To keep the values more manageable, we will use household income rather than the entire...
Given a set of samples which belong to two classes C1 and C2, assume C1 and...
Given a set of samples which belong to two classes C1 and C2, assume C1 and C2 are linearly separable, please prove that perceptron learning algorithm applied to the samples will terminate after a finite number of iterations.
Given two circles C1 and C2 in the same plane where C1 has a radius of...
Given two circles C1 and C2 in the same plane where C1 has a radius of a and C2 has a radius of b and the centers of the C1 and C2 have a distance of c apart. In terms of a,b, and c describe when the intersection of C1 and C2 would be zero points. prove this
1. Solve for the optimal values of C1 and C2 in the following optimization problem: MaxC1,C2...
1. Solve for the optimal values of C1 and C2 in the following optimization problem: MaxC1,C2 C11/2 + βC21/2 s.t. C1 + C2 /1 + r = Y1 + Y2/1 + r Hint: ∂C1/2 /∂C = 1/2C−1/2 When r goes up, how does C1 change? Does it increase or decrease?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT