ATM algorithm of giving money with limited amount of bank notes

后端 未结 2 609
无人及你
无人及你 2021-02-06 10:54

All the change-making problem in the web talk only about ideal situation where we have unlimited ammount of coins/banknotes of every kind.

I want to deal with situation

2条回答
  •  时光取名叫无心
    2021-02-06 11:35

    It can be done relatively easily, you just have to keep trying adding bank notes that left in every possibility and then discard possibilities, which already have more than you try to achieve.

    This is working code, values are "bank notes" values and in "ammounts" are ammounts of bank notes you have :

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    public class JavaApplication55 {
        int[] values = {10,20,50,100,200};
    
        public static void main(String[] args) {
            int[] values = {10,20,50,100,200};
            int[] ammounts = {10,10,10,10,10};
            List results = solutions(values, ammounts, new int[5], 180, 0);
            for (Integer[] result : results){
                System.out.println(Arrays.toString(result));
            }
    
        }
    
        public static List solutions(int[] values, int[] ammounts, int[] variation, int price, int position){
            List list = new ArrayList<>();
            int value = compute(values, variation);
            if (value < price){
                for (int i = position; i < values.length; i++) {
                    if (ammounts[i] > variation[i]){
                        int[] newvariation = variation.clone();
                        newvariation[i]++;
                        List newList = solutions(values, ammounts, newvariation, price, i);
                        if (newList != null){
                            list.addAll(newList);
                        }
                    }
                }
            } else if (value == price) {
                list.add(myCopy(variation));
            }
            return list;
        }    
    
        public static int compute(int[] values, int[] variation){
            int ret = 0;
            for (int i = 0; i < variation.length; i++) {
                ret += values[i] * variation[i];
            }
            return ret;
        }    
    
        public static Integer[] myCopy(int[] ar){
            Integer[] ret = new Integer[ar.length];
            for (int i = 0; i < ar.length; i++) {
                ret[i] = ar[i];
            }
            return ret;
        }
    }
    

    This code having this ouput (it is output for 10,20,50,100,200 bank notes, you have 10 of each of them and you want to get 180 in sum)

    [10, 4, 0, 0, 0]
    [9, 2, 1, 0, 0]
    [8, 5, 0, 0, 0]
    [8, 0, 2, 0, 0]
    [8, 0, 0, 1, 0]
    [7, 3, 1, 0, 0]
    [6, 6, 0, 0, 0]
    [6, 1, 2, 0, 0]
    [6, 1, 0, 1, 0]
    [5, 4, 1, 0, 0]
    [4, 7, 0, 0, 0]
    [4, 2, 2, 0, 0]
    [4, 2, 0, 1, 0]
    [3, 5, 1, 0, 0]
    [3, 0, 3, 0, 0]
    [3, 0, 1, 1, 0]
    [2, 8, 0, 0, 0]
    [2, 3, 2, 0, 0]
    [2, 3, 0, 1, 0]
    [1, 6, 1, 0, 0]
    [1, 1, 3, 0, 0]
    [1, 1, 1, 1, 0]
    [0, 9, 0, 0, 0]
    [0, 4, 2, 0, 0]
    [0, 4, 0, 1, 0]
    

提交回复
热议问题