Java的算法来解决供应商的机器的变化让“问题算法、机器、供应商、问题

2023-09-11 05:31:04 作者:不被喜欢

随着毕业我就去参加面试了java开发的作用,是做pretty的以及在技术考试,直到我碰到了这个问题。

如果我设置了一个自动贩卖机,它(为简单起见)返回£2更改。我如何将产生implemtation将列出的£2中的所有可能的组合。

有关,例如£1 +£1£1 + 50P + 50P,50P + 50P + 50P + 50P等..

如何可以列出的2.00£变化可能所有不同组合的自动售货机。

我开始写一些东西,这是香港专业教育学院想出了这么远。

它几乎工作,除了有人可以帮我找出为什么它不完全扩张。第二双眼睛会很感激。而且还可以优化的任何方式。

谢谢你们。

 私有静态无效printCoins(INT [] tempArray){

    的for(int i = 0; I< tempArray.length  -  1;我++){

//从打印出任何非分母硬币如停止我的数组
    如果(tempArray [I] 0){
 System.out.print(tempArray [I] +:);
    }
    的System.out.println(\ N);
  }
}


公共静态无效vendingMachine(){
    INT []面额= {200100,50,20,10,5,2,1};
    INT [] tempArray =新INT [50]; //硬币的组合制成
    INT总= 200;
    INT coinCombiIndex = 0,denomCoinIndex = 0;

    //而所有面额还没有被访问
 而(denomCoinIndex< denominations.length)

     //如果我有正确的改变
    如果(总 - 宗派[denomCoinIndex] == 0){
        tempArray [coinCombiIndex] =教派[denomCoinIndex]

        denomCoinIndex ++; //增量使下一次迭代开始低DENOM
        printCoins(tempArray); //返回硬币
    }

 //检查,看看新的总减硬币(分母)是否仍在> = 0
      否则如果(总 - 面额[denomCoinIndex]≥= 0){

            //如果是从总减去并添加硬币投币式组合阵列
            总=总 - 宗派[denomCoinIndex]
            tempArray [coinCombiIndex] =教派[denomCoinIndex]
            coinCombiIndex ++;

            }
        其他 {
        denomCoinIndex ++;

    }

    // printCoins(tempArray);

}
 
机器学习如何改善供应链

我的输出

  200:

100:100:

100:50:50:

100:50:20:20:10:

100:50:20:20:5:5:

100:50:20:20:5:2:2:1:
 

解决方案

要回答你的第二个问题:

试着只用{} 20,10,你会看到你的程序是非常不对的。

您试图改变我的recurence进入一个循环,我想这是最好的解决方案。但是,这是非常困难的事情,在一个循环(你就错过了很多的可能性)。

您可以尝试reinialise不同的约束,同时循环在例如添加一个新的每一个步骤,而在你的循环圈像这样的

 ,而(I< denominations.length){
             总= 200;
             denomCoinIndex =我;
             tempArray =新INT [1000];
             我++;
 

但它仍然不够...所以你需要重新添加一些循环,直到你把所有的情况。

我不认为你有一个while循环的方法是很好的一个位置。

最简单的方法是使用for循环像这样把所有可能的解决方案(从类似的问题,你的问题):

  INT总= 200;

               System.out的。的printf(季\ tdime \ tnickle \ tpenny \ TTO做出%D \ N,总);

                INT连击= 0;

                对于(INT Q = 0; Q< =总/ 25,Q ++)
                {
                    INT total_less_q =总 -  Q * 25;
                    对于(INT D = 0; D< = total_less_q / 10; d ++)
                    {
                        INT total_less_q_d = total_less_q  -  D * 10;
                        对于(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);
                            连击++;
                        }
                    }
                }

                System.out.printf(%d个组合的\ n,连击);
 

希望它可以帮助

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 was setting up a vending machine which (for simplicity) returned £2 change. How would i produce an implemtation that would list all the possible combinations of £2.

For e.g £1 + £1 , £1 + 50p + 50p, 50p + 50p + 50p + 50p and so on..

How could i list all the different combinations of £2.00 change possible by the vending machine.

I began to write something and this is what ive came up with so far.

Its almost working except can someone help me find out why its not expanding fully. A second pair of eyes will be grateful. And also any ways it can be optimised.

Thanks guys.

 private static void printCoins(int[] tempArray) {

    for (int i = 0; i < tempArray.length - 1; i++){

// to stop my array from printing out any non-denominator coins e.g  
    if (tempArray[i] > 0){
 System.out.print(tempArray[i] + ": ");
    }
    System.out.println("\n");
  }
}


public static void vendingMachine() {
    int[] denominations = {200,100, 50, 20, 10, 5, 2, 1};
    int[] tempArray = new int[50]; //combination of coins made
    int total = 200;
    int coinCombiIndex = 0, denomCoinIndex = 0;

    // whilst all denominations havent been visited
 while (denomCoinIndex < denominations.length)

     // if i have the correct change
    if (total - denominations[denomCoinIndex] == 0){
        tempArray[coinCombiIndex] = denominations[denomCoinIndex];

        denomCoinIndex++;  //increment so that next iteration starts with lower denom
        printCoins(tempArray); // return coins
    }

 // checks to see whether new total minus coin (denominator) is still >= 0
      else if (total - denominations[denomCoinIndex] >= 0) {

            // if so SUBTRACT from total and ADD coin to coin-combination-array
            total = total - denominations[denomCoinIndex];
            tempArray[coinCombiIndex] = denominations[denomCoinIndex];
            coinCombiIndex++;

            }
        else {
        denomCoinIndex++;

    }

    //  printCoins(tempArray);

}

my output

200: 

100: 100: 

100: 50: 50: 

100: 50: 20: 20: 10: 

100: 50: 20: 20: 5: 5: 

100: 50: 20: 20: 5: 2: 2: 1: 

解决方案

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

 
精彩推荐
图片推荐