fork download
  1. import java.util.*;
  2. import java.lang.*;
  3.  
  4. /**
  5.  * To answer the question "How many combinations of coins add up to $1.00". US
  6.  * currency is assumed. Reptition is assumed. Does not calculate permutations
  7.  * where position is important.
  8.  **/
  9. class Main
  10. {
  11. public static int PENNY = 1;
  12. public static int NICKEL = 5;
  13. public static int DIME = 10;
  14. public static int QUARTER = 25;
  15. public static int HALF = 50;
  16.  
  17. public static int PENNYV = 1;
  18. public static int NICKELV = 5;
  19. public static int DIMEV = 10;
  20. public static int QUARTERV = 25;
  21. public static int HALFV = 50;
  22.  
  23. private int combinations = 0;
  24.  
  25. public static void main (String[] args) throws java.lang.Exception
  26. {
  27. Main main = new Main();
  28. main.run();
  29. }
  30.  
  31. private void run(){
  32. // iterate through each coin and add up the combinations
  33. // pennies
  34. int current = 0;
  35. for(int j = 0; j <= 20; j++){ // nickels
  36.  
  37. NICKELV = (j * NICKEL);
  38.  
  39. for(int k = 0; k <= 10; k++){ // dimes
  40. DIMEV = (k * DIME);
  41.  
  42. for(int m = 0; m <= 4; m++){ // quarters
  43. QUARTERV = (m * QUARTER);
  44.  
  45. for(int n = 0; n <= 2; n++){ // halves
  46. HALFV = (n * HALF);
  47. current = HALFV + QUARTERV + DIMEV + NICKELV;
  48. if(current == 100){
  49. combinations++; // increment combination count
  50. }
  51.  
  52. }
  53. }
  54. }
  55. }
  56.  
  57.  
  58. System.out.println("There are " + combinations + " possible combinations.");
  59. }
  60.  
  61. }
Success #stdin #stdout 0.06s 380224KB
stdin
Standard input is empty
stdout
There are 40 possible combinations.