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. private int combinations = 0;
  18.  
  19. public static void main (String[] args) throws java.lang.Exception
  20. {
  21. Main main = new Main();
  22. main.run();
  23. }
  24.  
  25. private void run(){
  26. // iterate through each coin and add up the combinations
  27. // pennies
  28. int current = 0;
  29. for(int i = 0; i <= 100; i++){ // pennies
  30. current = i * PENNY;
  31.  
  32. for(int j = 0; j <= 20; j++){ // nickels
  33. current = current + (j * NICKEL);
  34.  
  35. for(int k = 0; k <= 10; k++){ // dimes
  36. current = current + (k * DIME);
  37.  
  38. for(int m = 0; m <= 4; m++){ // quarters
  39. current = current + (m * QUARTER);
  40.  
  41. for(int n = 0; n <= 2; n++){ // halves
  42. current = current + (n * HALF);
  43.  
  44. if(current == 100){
  45. combinations++; // increment combination count
  46. }
  47. current = 0; // start again
  48. }
  49. }
  50. }
  51. }
  52. }
  53.  
  54. System.out.println("There are " + combinations + " possible combinations.");
  55. }
  56.  
  57. }
Success #stdin #stdout 0.07s 380160KB
stdin
Standard input is empty
stdout
There are 142209 possible combinations.