A seal in which coins are used to make a given amount

I am trying to use recursion to find the minimum number of coins to get a given amount. I have a code that can display the minimum number of coins required, but I can’t find a way to print which coins were used to solve this issue. I searched and found similar examples, but I cannot apply it correctly to this.

Here is what I still have:

import java.util.*;

public class Coins{

    public static int findMinCoins(int[] currency, int amount) {
        int i, j, min, tempSolution;

        min = amount;

        for (i = 0; i < currency.length; i++) {
            if (currency[i] == amount) {
                return 1;
            }
        }

        for (j = 1; j <= (amount / 2); j++) {
            tempSolution = findMinCoins(currency, j) + findMinCoins(currency, amount - j);
            if (tempSolution < min) {
                min = tempSolution;
            }
        }
        return min;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int[] USA =
        {1, 5, 10, 25, 50};
        System.out.println("Please enter an integer amount.");
        int amount = in.nextInt();
        int minCoins = findMinCoins(USA, amount);
        System.out.println("The minimum number of coins to make " + amount + " in United States currency is " + minCoins + ".");
        System.out.println("The coins used were:");
        /*Print coins used to find minCoins.*/
        in.close();
    }
}

Example code that still works:

Please enter an integer amount.
17
The minimum number of coins to make 17 in United States currency is 4.
The coins used were:

If someone can give me some idea of ​​how to do this, that would be very appreciated.

+4
source share
4 answers

, , . public static int findMinCoins(arg1, arg2) , ( ), .

public static int findMinCoins(int[] currency, int amount) {
    int min = findMinCoins(currency, amount, 0);
    System.out.print("The coins used were: ");
    return min;
}
private static int findMinCoins(int[] currency, int amount, int min){
    int number, value1, value2;
    int min1 = min;
    for(int i=currency.length-1; i>=0; i--) {
        if (amount>=currency[i]){
            amount = amount - currency[i];
            System.out.print(currency[i] + " ");
            min1 = findMinCoins(currency, amount, min1);
            return ++min1;
        }
    }
    return min1;
}
+2

, , . , , , .

, . , Arrays.sort(currency).

public class FindMinimumCoinsTest {

  @Test
  public void test() throws Exception {
      int[] USA = { 1, 5, 10, 25, 50 };
      assertEquals(2, findMinCoins(USA, 11));
      assertEquals(4, findMinCoins(USA, 8));
      assertEquals(4, findMinCoins(USA, 111));
      assertEquals(3, findMinCoins(USA, 27));
  }

  public static int findMinCoins(int[] currency, int amount) {
      int coins = 0;
      int sum = 0;
      int value, n;

      for (int i = currency.length - 1; i >= 0; i--) {
          value = currency[i];
          n = (amount - sum) / value;
          if (n > 0) {
              coins += n;
              sum += n * value;
          }
      }
      return coins;
    }
}

, ;)

0

(, ...). , {1,5,10,25,50} ( )

:

1 int. ( , :

public class coins {

    public static void main(String[] args) {
        // arrays of coin types
        int[] coinTypes = { 0, 1, 5, 10, 25, 50 };
        // arrays are references, so changing them
        // inside the recursive function will 'really' change
        int[] price = {11}; // sample input
        tryBigger(price, coinTypes, 0);
    }

    // tries to see if higher coin is possible by passing array
    // of coin types and recursively call until the end of array
    public static void tryBigger(int[] price, int[] theCoins, int index) {
        if (index < theCoins.length-1){
            // until last element
            if (price[0] > theCoins[index]){
                tryBigger(price, theCoins, ++index);
            }
        }
        // find amount of this coin
        int thisCoin = price[0]/theCoins[index];
        // remove the amount already got before
        price[0] = price[0] - thisCoin*theCoins[index];
        System.out.println(thisCoin + " coins of " + theCoins[index]);
        return;

    }
}
0

, , 1 min. , , , , .

; Java, .

if (currency[i] == amount){
    return [1,i];

...

temp1 = findMinCoins(currency,j);
temp2 = findMinCoins(currency,amount - j);

if(temp1[0] + temp2[0] < min[0]){
    min = [temp1[0] + temp2[0]] concatenated with temp1[1..] and temp2[1..]
0

Source: https://habr.com/ru/post/1612150/


All Articles