Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public interface Exchanging {
- Map<Integer, Integer> exchange();
- }
- public class Exchange implements Exchanging {
- /**
- * Money which should be exchanging.
- */
- private final int sumForExchange;
- /**
- * Nominal coins.
- */
- private final int[] denominationСoins;
- /**
- * Default constructor.
- *
- * @param sumForExchange money which should be exchanging.
- * @param denominationСoins array with nominal coins.
- */
- public Exchange(final int sumForExchange, final int... denominationСoins) {
- this.denominationСoins = denominationСoins;
- this.sumForExchange = sumForExchange;
- }
- /**
- * Exchange big money on less coins.
- *
- * @return map with coins. Nominal is key, amount coin is value.
- */
- @Override
- public Map<Integer, Integer> exchange() {
- Map<Integer, Integer> result = new HashMap<>();
- int[] coins = sort();
- int residue = sumForExchange;
- for (int i = 0; i != coins.length; i++) {
- ExchangeByValue exchangeByValue = new ExchangeByValue(
- residue, coins[i]);
- // how much coins content current residue for denominationСoins[i].
- int amountCoin = exchangeByValue.exchange();
- result.put(coins[i], amountCoin);
- // update residue.
- residue = exchangeByValue.getResidue();
- }
- return result;
- }
- /**
- * Sorting array with coins nominal.
- *
- * @return arr sort by descending.
- */
- private int[] sort() {
- int[] result = denominationСoins;
- for (int i = result.length - 1; i >= 0; i--) {
- for (int j = 0; j < i; j++) {
- if (result[j] < result[j + 1]) {
- int temp = result[j];
- result[j] = result[j + 1];
- result[j + 1] = temp;
- }
- }
- }
- return result;
- }
- /**
- * Calculate residue and amount coins.
- */
- private class ExchangeByValue {
- /**
- * Residue for division on coins.
- */
- private final int denomination;
- /**
- * Nominal coin.
- */
- private final int divider;
- /**
- * Default constructor.
- *
- * @param denomination Residue for division on coins.
- * @param divider Nominal coin.
- */
- private ExchangeByValue(final int denomination, final int divider) {
- this.denomination = denomination;
- this.divider = divider;
- }
- /**
- * @return amount coins which may by get for this.denomination.
- */
- private int exchange() {
- return denomination / divider;
- }
- /**
- * @return residue for future exchange.
- */
- private int getResidue() {
- return denomination % divider;
- }
- }
- }
- public class Way
- {
- private final Integer[] coins;
- public Way()
- {
- coins = new Integer[0];
- }
- public Way(Way way)
- {
- coins = new Integer[way.coins.length + 1];
- System.arraycopy(way.coins, 0, coins, 0, way.coins.length);
- }
- public void add(int coin)
- {
- coins[coins.length - 1] = coin;
- }
- public void print()
- {
- for (int i = 0; i < coins.length - 1; i++)
- {
- System.out.print(coins[i] + " ");
- }
- System.out.println();
- }
- }
- import java.util.*;
- public class WaysGroup
- {
- private final List<Way> ways;
- public WaysGroup()
- {
- ways = new ArrayList<>();
- }
- public void add(Way way)
- {
- ways.add(way);
- }
- public void add(int coin)
- {
- for (Way way : ways)
- {
- way.add(coin);
- }
- }
- public void add(WaysGroup group)
- {
- if (group == null)
- {
- return;
- }
- for (Way way : group.ways)
- {
- ways.add(new Way(way));
- }
- }
- public void print()
- {
- for (Way way : ways)
- {
- way.print();
- }
- }
- }
- public WaysGroup getAllExchanges()
- {
- int[] coins = denominationСoins;
- WaysGroup[][] waysGroups = new WaysGroup[coins.length][sumForExchange + 1];
- waysGroups[0][0] = new WaysGroup();
- waysGroups[0][0].add(new Way());
- for (int i = 0; i < sumForExchange; i++)
- {
- for (int j = 0; j < coins.length; j++)
- {
- for (int k = j; k < coins.length; k++)
- {
- if (i + coins[k] <= sumForExchange)
- {
- if (waysGroups[k][i + coins[k]] == null)
- {
- waysGroups[k][i + coins[k]] = new WaysGroup();
- }
- waysGroups[k][i + coins[k]].add(waysGroups[j][i]);
- }
- }
- if (i + coins[j] <= sumForExchange)
- {
- waysGroups[j][i + coins[j]].add(coins[j]);
- }
- waysGroups[j][i] = null;
- }
- }
- WaysGroup result = new WaysGroup();
- for (int i = 0; i < coins.length; i++)
- {
- result.add(waysGroups[i][sumForExchange]);
- }
- return result;
- }
- public static void main(String[] args)
- {
- Exchange exchange = new Exchange(10, 1, 2, 3);
- WaysGroup allWays = exchange.getAllExchanges();
- allWays.print();
- }
- int money = 200;
- List<Integer> coins = Arrays.asList(5, 2, 1);
- getCombinations(money, coins).forEach(System.out::println);
- public static Set<List<Integer>> getCombinations(int money, List<Integer> coins){
- Set<List<Integer>> result = new HashSet<>();
- for (int i = 0; i <= coins.size(); i++)
- getCombinations(money, coins.subList(0, i), new ArrayList<>(), result);
- return result;
- }
- private static void getCombinations(int money,
- List<Integer> coins,
- List<Integer> buffer,
- Set<List<Integer>> result) {
- if (money == 0) {
- result.add(buffer);
- return;
- }
- if (money < 0)
- return;
- for (int i = 0; i < coins.size(); i++) {
- List<Integer> intermediate = new ArrayList<>(buffer);
- intermediate.add(coins.get(i));
- getCombinations(
- money - coins.get(i),
- coins.subList(0, i + 1),
- intermediate,
- result);
- }
- }
Add Comment
Please, Sign In to add comment