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.  
  12. public static void main (String[] args) throws java.lang.Exception
  13. {
  14. Main main = new Main();
  15. main.run();
  16. }
  17.  
  18. private void run(){
  19. // initialize array so that c[0] = 1 and c[i] = 0 for all i > 0
  20. int[] c = new int[101];
  21. c[0] = 1;
  22. for(int i = 1; i < 101; i++){
  23. c[i] = 0;
  24. }
  25.  
  26. // For k=1,5,10,25,50 (in any order) do:
  27. int[] k = new int[5];
  28.  
  29. k[0] = 1;
  30. k[1] = 5;
  31. k[2] = 10;
  32. k[3] = 25;
  33. k[4] = 50;
  34.  
  35. for (int m = 0; m < 5; m++){
  36. int max = (100 - k[m]);
  37. //for i=0,1,…,100-k (in this order) do:
  38. for(int i = 0; i <= max; i++){
  39. //add c[i] to c[i+k]
  40. c[i+k[m]] += c[i];
  41. }
  42. }
  43.  
  44. System.out.println("There are " + c[100] + " possible combinations.");
  45. }
  46.  
  47. }
Success #stdin #stdout 0.07s 380160KB
stdin
Standard input is empty
stdout
There are 292 possible combinations.