Guest User

Untitled

a guest
Jan 18th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.34 KB | None | 0 0
  1. public interface Exchanging {
  2. Map<Integer, Integer> exchange();
  3. }
  4.  
  5. public class Exchange implements Exchanging {
  6. /**
  7. * Money which should be exchanging.
  8. */
  9. private final int sumForExchange;
  10. /**
  11. * Nominal coins.
  12. */
  13. private final int[] denominationСoins;
  14.  
  15. /**
  16. * Default constructor.
  17. *
  18. * @param sumForExchange money which should be exchanging.
  19. * @param denominationСoins array with nominal coins.
  20. */
  21. public Exchange(final int sumForExchange, final int... denominationСoins) {
  22. this.denominationСoins = denominationСoins;
  23. this.sumForExchange = sumForExchange;
  24. }
  25.  
  26. /**
  27. * Exchange big money on less coins.
  28. *
  29. * @return map with coins. Nominal is key, amount coin is value.
  30. */
  31. @Override
  32. public Map<Integer, Integer> exchange() {
  33. Map<Integer, Integer> result = new HashMap<>();
  34. int[] coins = sort();
  35. int residue = sumForExchange;
  36.  
  37. for (int i = 0; i != coins.length; i++) {
  38. ExchangeByValue exchangeByValue = new ExchangeByValue(
  39. residue, coins[i]);
  40.  
  41. // how much coins content current residue for denominationСoins[i].
  42. int amountCoin = exchangeByValue.exchange();
  43. result.put(coins[i], amountCoin);
  44. // update residue.
  45. residue = exchangeByValue.getResidue();
  46. }
  47. return result;
  48. }
  49.  
  50. /**
  51. * Sorting array with coins nominal.
  52. *
  53. * @return arr sort by descending.
  54. */
  55. private int[] sort() {
  56. int[] result = denominationСoins;
  57. for (int i = result.length - 1; i >= 0; i--) {
  58. for (int j = 0; j < i; j++) {
  59. if (result[j] < result[j + 1]) {
  60. int temp = result[j];
  61. result[j] = result[j + 1];
  62. result[j + 1] = temp;
  63. }
  64. }
  65. }
  66. return result;
  67. }
  68.  
  69. /**
  70. * Calculate residue and amount coins.
  71. */
  72. private class ExchangeByValue {
  73. /**
  74. * Residue for division on coins.
  75. */
  76. private final int denomination;
  77. /**
  78. * Nominal coin.
  79. */
  80. private final int divider;
  81.  
  82. /**
  83. * Default constructor.
  84. *
  85. * @param denomination Residue for division on coins.
  86. * @param divider Nominal coin.
  87. */
  88. private ExchangeByValue(final int denomination, final int divider) {
  89. this.denomination = denomination;
  90. this.divider = divider;
  91. }
  92.  
  93. /**
  94. * @return amount coins which may by get for this.denomination.
  95. */
  96. private int exchange() {
  97. return denomination / divider;
  98. }
  99.  
  100. /**
  101. * @return residue for future exchange.
  102. */
  103. private int getResidue() {
  104. return denomination % divider;
  105. }
  106. }
  107. }
  108.  
  109. public class Way
  110. {
  111. private final Integer[] coins;
  112.  
  113. public Way()
  114. {
  115. coins = new Integer[0];
  116. }
  117.  
  118. public Way(Way way)
  119. {
  120. coins = new Integer[way.coins.length + 1];
  121. System.arraycopy(way.coins, 0, coins, 0, way.coins.length);
  122. }
  123.  
  124. public void add(int coin)
  125. {
  126. coins[coins.length - 1] = coin;
  127. }
  128.  
  129. public void print()
  130. {
  131. for (int i = 0; i < coins.length - 1; i++)
  132. {
  133. System.out.print(coins[i] + " ");
  134. }
  135. System.out.println();
  136. }
  137. }
  138.  
  139. import java.util.*;
  140.  
  141. public class WaysGroup
  142. {
  143. private final List<Way> ways;
  144.  
  145. public WaysGroup()
  146. {
  147. ways = new ArrayList<>();
  148. }
  149.  
  150. public void add(Way way)
  151. {
  152. ways.add(way);
  153. }
  154.  
  155. public void add(int coin)
  156. {
  157. for (Way way : ways)
  158. {
  159. way.add(coin);
  160. }
  161. }
  162.  
  163. public void add(WaysGroup group)
  164. {
  165. if (group == null)
  166. {
  167. return;
  168. }
  169. for (Way way : group.ways)
  170. {
  171. ways.add(new Way(way));
  172. }
  173. }
  174.  
  175. public void print()
  176. {
  177. for (Way way : ways)
  178. {
  179. way.print();
  180. }
  181. }
  182. }
  183.  
  184. public WaysGroup getAllExchanges()
  185. {
  186. int[] coins = denominationСoins;
  187. WaysGroup[][] waysGroups = new WaysGroup[coins.length][sumForExchange + 1];
  188. waysGroups[0][0] = new WaysGroup();
  189. waysGroups[0][0].add(new Way());
  190. for (int i = 0; i < sumForExchange; i++)
  191. {
  192. for (int j = 0; j < coins.length; j++)
  193. {
  194. for (int k = j; k < coins.length; k++)
  195. {
  196. if (i + coins[k] <= sumForExchange)
  197. {
  198. if (waysGroups[k][i + coins[k]] == null)
  199. {
  200. waysGroups[k][i + coins[k]] = new WaysGroup();
  201. }
  202. waysGroups[k][i + coins[k]].add(waysGroups[j][i]);
  203. }
  204. }
  205. if (i + coins[j] <= sumForExchange)
  206. {
  207. waysGroups[j][i + coins[j]].add(coins[j]);
  208. }
  209. waysGroups[j][i] = null;
  210. }
  211. }
  212. WaysGroup result = new WaysGroup();
  213. for (int i = 0; i < coins.length; i++)
  214. {
  215. result.add(waysGroups[i][sumForExchange]);
  216. }
  217. return result;
  218. }
  219.  
  220. public static void main(String[] args)
  221. {
  222. Exchange exchange = new Exchange(10, 1, 2, 3);
  223. WaysGroup allWays = exchange.getAllExchanges();
  224. allWays.print();
  225. }
  226.  
  227. int money = 200;
  228. List<Integer> coins = Arrays.asList(5, 2, 1);
  229. getCombinations(money, coins).forEach(System.out::println);
  230.  
  231. public static Set<List<Integer>> getCombinations(int money, List<Integer> coins){
  232. Set<List<Integer>> result = new HashSet<>();
  233.  
  234. for (int i = 0; i <= coins.size(); i++)
  235. getCombinations(money, coins.subList(0, i), new ArrayList<>(), result);
  236.  
  237. return result;
  238. }
  239.  
  240. private static void getCombinations(int money,
  241. List<Integer> coins,
  242. List<Integer> buffer,
  243. Set<List<Integer>> result) {
  244.  
  245. if (money == 0) {
  246. result.add(buffer);
  247. return;
  248. }
  249. if (money < 0)
  250. return;
  251.  
  252. for (int i = 0; i < coins.size(); i++) {
  253. List<Integer> intermediate = new ArrayList<>(buffer);
  254. intermediate.add(coins.get(i));
  255. getCombinations(
  256. money - coins.get(i),
  257. coins.subList(0, i + 1),
  258. intermediate,
  259. result);
  260. }
  261. }
Add Comment
Please, Sign In to add comment