Advertisement
Guest User

Untitled

a guest
Jul 25th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.32 KB | None | 0 0
  1. decimal i;
  2. i = decimal.Parse(txtAmountReq.Text);
  3. decimal totalAmount = Convert.ToDecimal(txtAmountReq);
  4.  
  5.  
  6. int[] denomBills = { 50, 20, 10, 5, 1 };
  7. int[] numberOfBills = new int[5];
  8. decimal[] denomCoins = { .25m, .10m, .05m, .01m };
  9. int[] numberOfCoins = new int[4];
  10.  
  11. //For loop for amount of bills
  12. for (numberOfBills[0] = 0; totalAmount >= 50; numberOfBills[0]++)
  13. {
  14. totalAmount = totalAmount - 50;
  15. }
  16. for (numberOfBills[1] = 0; totalAmount < 20; numberOfBills[1]++)
  17. {
  18. totalAmount = totalAmount - 20;
  19. }
  20. for (numberOfBills[2] = 0; totalAmount < 10; numberOfBills[2]++)
  21. {
  22. totalAmount = totalAmount - 10;
  23. }
  24. for (numberOfBills[3] = 0; totalAmount < 5; numberOfBills[3]++)
  25. {
  26. totalAmount = totalAmount - 5;
  27. }
  28. for (numberOfBills[4] = 0; totalAmount <= 0; numberOfBills[4]++)
  29. {
  30. totalAmount = totalAmount - 1;
  31. }
  32.  
  33.  
  34. //For loop for the amount of coins
  35. for (numberOfCoins[0] = 0; totalAmount >= .25m; numberOfBills[0]++)
  36. {
  37. totalAmount = totalAmount - .25m;
  38. }
  39. for (numberOfBills[1] = 0; totalAmount < .10m; numberOfBills[1]++)
  40. {
  41. totalAmount = totalAmount - .10m;
  42. }
  43. for (numberOfBills[2] = 0; totalAmount < .05m; numberOfBills[2]++)
  44. {
  45. totalAmount = totalAmount - .05m;
  46. }
  47. for (numberOfBills[3] = 0; totalAmount < .01m; numberOfBills[3]++)
  48. {
  49. totalAmount = totalAmount - .01m;
  50. }
  51.  
  52. txt50.Text = Convert.ToString(numberOfBills[0]);
  53. txt20.Text = Convert.ToString(numberOfBills[1]);
  54. txt10.Text = Convert.ToString(numberOfBills[2]);
  55. txt5.Text = Convert.ToString(numberOfBills[3]);
  56. txt1.Text = Convert.ToString(numberOfBills[4]);
  57.  
  58. txtQuarter.Text = Convert.ToString(numberOfCoins[0]);
  59. txtDime.Text = Convert.ToString(numberOfCoins[1]);
  60. txtNickel.Text = Convert.ToString(numberOfCoins[2]);
  61. txtPenny.Text = Convert.ToString(numberOfCoins[3]);
  62. }
  63.  
  64. //find all the combinations
  65. private void findAllCombinationsRecursive(String tsoln,
  66. int startIx,
  67. int remainingTarget,
  68. CoinChangeAnswer answer) {
  69. for(int i=startIx; i<answer.denoms.length ;i++) {
  70. int temp = remainingTarget - answer.denoms[i];
  71. String tempSoln = tsoln + "" + answer.denoms[i]+ ",";
  72. if(temp < 0) {
  73. break;
  74. }
  75. if(temp == 0) {
  76. // reached the answer hence quit from the loop
  77. answer.allPossibleChanges.add(tempSoln);
  78. break;
  79. }
  80. else {
  81. // target not reached, try the solution recursively with the
  82. // current denomination as the start point.
  83. findAllCombinationsRecursive(tempSoln, i, temp, answer);
  84. }
  85. }
  86. }
  87.  
  88. public CoinChangeAnswer findOptimalChange(int target, int[] denoms) {
  89. CoinChangeAnswer soln = new CoinChangeAnswer(target,denoms);
  90. StringBuilder sb = new StringBuilder();
  91.  
  92. // initialize the solution structure
  93. for(int i=0; i<soln.OPT[0].length ; i++) {
  94. soln.OPT[0][i] = i;
  95. soln.optimalChange[0][i] = sb.toString();
  96. sb.append(denoms[0]+" ");
  97. }
  98.  
  99. // Read through the following for more details on the explanation
  100. // of the algorithm.
  101. // http://condor.depaul.edu/~rjohnson/algorithm/coins.pdf
  102. for(int i=1 ; i<denoms.length ; i++) {
  103. for(int j=0; j<target+1 ; j++) {
  104. int value = j;
  105. int targetWithPrevDenomiation = soln.OPT[i-1][j];
  106. int ix = (value) - denoms[i];
  107. if( ix>=0 && (denoms[i] <= value )) {
  108. int x2 = denoms[i] + soln.OPT[i][ix];
  109. if(x2 <= target && (1+soln.OPT[i][ix] < targetWithPrevDenomiation)) {
  110. String temp = soln.optimalChange[i][ix] + denoms[i] + " ";
  111. soln.optimalChange[i][j] = temp;
  112. soln.OPT[i][j] = 1 + soln.OPT[i][ix];
  113. } else {
  114. soln.optimalChange[i][j] = soln.optimalChange[i-1][j]+ " ";
  115. soln.OPT[i][j] = targetWithPrevDenomiation;
  116. }
  117. } else {
  118. soln.optimalChange[i][j] = soln.optimalChange[i-1][j];
  119. soln.OPT[i][j] = targetWithPrevDenomiation;
  120. }
  121. }
  122. }
  123. return soln;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement