Java Algorithm to solve vendor machine 'change giving' problem

前端 未结 6 787
太阳男子
太阳男子 2021-01-16 22:10

As a graduate I went for an interview for a java development role and was doing pretty well in the technical examinations until i came up against this question.

If i

相关标签:
6条回答
  • 2021-01-16 22:19

    Reframe the problem as follows: Given a standard (modern) car odometer that registers 6 digits with no fractions, find all possible values where the sum of the digits is some value, say 15. If you can solve that, you can solve the given problem.

    0 讨论(0)
  • 2021-01-16 22:20

    To answer your second question:

    Try with only {20,10} you will see that your program is really not right .

    You tried to transform my recurence into a loop, and I guess it's the best solution. But it's very difficult to do that in one loop (you're missing a lot of possibilities).

    You can try to reinialise the while loop with different constraint at each step for example adding a new while loop around your loop like this one

    while (i<denominations.length){
                 total=200;
                 denomCoinIndex=i;
                 tempArray = new int[1000]; 
                 i++;
    

    but it's still not enough ... so you will need to add some loop again until you treat all the cases.

    I don't think your approach with a while loop is the good one here.

    The easiest way would be to use for loop like this to treat all the possible solutions (from the similar question to your question):

     int total = 200;
    
                   System.out. printf("quarter\tdime\tnickle\tpenny\tto make %d\n", total);
    
                    int combos = 0;
    
                    for (int q = 0; q <= total / 25; q++)
                    {
                        int total_less_q = total - q * 25;
                        for (int d = 0; d <= total_less_q / 10; d++)
                        {
                            int total_less_q_d = total_less_q - d * 10;
                            for (int n = 0; n <= total_less_q_d / 5; n++)
                            {
                                int p = total_less_q_d - n * 5;
                                System.out.printf("%d\t%d\t%d\t%d\n", q, d, n, p);
                                combos++;
                            }
                        }
                    }
    
                    System.out.printf("%d combinations\n", combos);
    

    Hope it helps

    0 讨论(0)
  • 2021-01-16 22:23
    public class VendeningMachine
    {
        public static final int[] coins = {1, 5, 10, 25};
        public static final int[] coinMax = {200, 40, 20, 8};
        public static final int[] coinsString = { "Penny", "Nickle", "Dime", "Quarter"};
    
        public static void main(String[] args)
        {
    
        }
    
    
        public static void vendingMachine()
        {
            for ( int a = 0; a <= coinMax[0]; a++ ) {
                for ( int b = 0; b <= coinMax[1]; b++ ) {
                    for ( int c = 0; c < coinMax[2]; c++ ) {
                        for ( int d = 0; d < coinMax[3]; d++ ) {
                            checkCoins(a, b, c, d);
                        }
                    }
                }
            }
        }
    
        public static void checkCoins(int penny, int nickle, int dime, int quarter)
        {
            int total = coins[0] * penny + coins[1] * nickle + coins[2] * dime + coins[3] * quarter;
    
            if ( total == 200 )
                printCoins(penny, nickle, dime, quarter);
    
        }
    
        public static void printCoins(int penny, int nickle, int dime, int quarter)
        {
            System.out.println("Penney : " + penny + " Nickle: " + nickle + " Dime: " + dime + " Quarter: " + quarter);
        }
    
    }enter code here
    
    0 讨论(0)
  • 2021-01-16 22:25

    Probably get started on looking at Dynamic programming. However you could any approach problem for that matter trivially. How would you do that manually ??. Jot down the steps. Convert it into an algorithm yourself. Probably studying Permutations & Combinations would help for better understanding of the problem you have stated.

    Hope it helps.

    0 讨论(0)
  • 2021-01-16 22:26

    Algorithm F on (in-text) page 7 is exactly what you're looking for. :)

    http://www-cs-staff.stanford.edu/~uno/fasc3a.ps.gz

    0 讨论(0)
  • 2021-01-16 22:46

    Let say your possible coins are x1 ...xn :

    if you're asked to print all the possiblities for $2 then you could print recursively all the possibilities for :

    2-x1
    2-x2
    ..
    2-xn
    

    Yoou will eventually get all the solutions

    You can initialise this recursive process with amount-xi = 0 then print

    0 讨论(0)
提交回复
热议问题