Define change combinations for a given amount

My purpose is to write an algorithm using brute force to determine the number of different methods associated with combinations of changes for a given amount. The change will be made using the following coins: a penny (1 cent), nickel (5 cents), a penny (10 cents) and a quarter (25 cents).

eg.

Entrance: 16 (this means a change of 16 cents)

Output: can be produced in 6 different ways, and they are:

  • 16 pennies.
  • 11 pennies, 1 nickel
  • 6 pennies, 1 penny
  • 6 pennies, 2 nickels
  • 1 penny, 3 nickels
  • 1 penny, 1 nickel, 1 penny

My algorithm should make all possible combinations of changes for a certain number of changes.


, . , , .

+5
5

Ok. . .

c cents. c

c = p * PENNY + n * NICKEL + d * DIME + q * QUARTER

,

c = ( p * 1 ) + ( n * 5 ) + ( d * 10 ) + ( q * 25 )

p, n, d q, c. , p in [0, maximumPennies] n in [0, maximumNickels]. n d in [0, maximumDimes]. d q in [0, maximumQuarters].

p in [0, maximumPennies] AND c >= p
  |
  +- n in [0, maximumNickels] AND c >= p + 5n
       |
       +- d in [0, maximumDimes] AND c >= p + 5n + 10d
            |
            +- q in [0, maximumQuarters] AND c >= p + 5n + 10d + 25q

.

+4

, , . , .

:

1. find out the number of ways you can make using penny only.
2. do the same using penny and nickel only. (this includes step 1 also)
3. the same using penny, nickel and dime only (including step 2).
4. using all the coins (with all previous steps).

1 , .

2 :

number of ways to make n cent using penny and nickel = 
    number of ways to make (n - [1 nickel]) using penny and nickel
    + number of ways to make n cent using penny only

3:

number of ways to make n cent using penny, nickel and dime = 
    number of ways to make (n - [1 dime]) using penny, nickel and dime
    + number of ways to make n cent using penny and nickel only

4 .

, : 0 (.. ), .

+2

, -, , .

, ( , ), . , .

+1

Try using recursion on this. Your function should take two parameters - the maximum value that you are allowed to use, and the remaining amount (you must first avoid repeating). Make the function this way: if it is in a trivial case (for example, 1, 5, 10, and you are allowed to take a penny, no nickel, or a penny, respectively), print a trivial solution. Also, for each case, try to take one coin of all allowed types (for example, no more than the maximum allowed) and continue recursively.

Hope this helps.

0
source
public class PrintAllCoinCombinations {


    static int findChange(int arr[], int index , int value, String str){

        if(value == 0){
            System.out.println(str);
            return 1;
        }
        if(index<0){
            return 0;
        }
        if(value<0){
            return 0;
        }

        int excl  = findChange(arr,index-1,value,str);

        str += " "+ arr[index];

        int incl = findChange(arr,index,value-arr[index],str);

        return incl + excl;

    }

    public static void main(String [] arg){
        int arr[] = {1,5,10,25};
        String s = "";
        int result = findChange(arr,3,16,s);
        System.out.println(result);
    }
}
-1
source

All Articles