fork download
  1. import java.util.*;
  2. import java.lang.*;
  3. import java.io.*;
  4.  
  5. class Ideone
  6. {
  7. public static void main(String[] args)
  8. {
  9. try(Scanner in = new Scanner(System.in))
  10. {
  11. while(true)
  12. {
  13. System.out.printf("Weight of Coins (gram) : ");
  14. if (!in.hasNextDouble()) break;
  15. n(in.nextDouble());
  16. }
  17. }
  18. }
  19.  
  20. static void n(double target)
  21. {
  22. ArrayList<CoinSet> result = new ArrayList<CoinSet>();
  23. long s = System.nanoTime();
  24. n(target, new int[Coin.count], 0, 0, 0, result);
  25. result.sort(Comparator.comparing(x -> x.value));
  26. long e = System.nanoTime();
  27.  
  28. System.out.printf("%nCoin Weight : %f g%n", target);
  29. System.out.printf("Number of List : %d%n", result.size());
  30. System.out.printf("amount(yen) : 1yen 5yen 10yen 50yen 100yen 500yen%n");
  31. System.out.printf("---------------------------------------------------------%n");
  32. // for(CoinSet set : result)
  33. // {
  34. // System.out.println(set);
  35. // }
  36. System.out.printf("%,dns%n%n", e - s);
  37. }
  38.  
  39. static class Coin
  40. {
  41. static int count;
  42. static Coin[] list =
  43. {
  44. new Coin(1, 1.0D),
  45. new Coin(5, 3.7D),
  46. new Coin(10, 4.5D),
  47. new Coin(50, 4.0D),
  48. new Coin(100, 4.8D),
  49. new Coin(500, 7.0D),
  50. };
  51.  
  52. final int id;
  53. final int value;
  54. final double weight;
  55.  
  56. Coin(int value, double weight)
  57. {
  58. this.id = count++;
  59. this.value = value;
  60. this.weight = weight;
  61. }
  62. }
  63.  
  64. static class CoinSet
  65. {
  66. int[] num;
  67. int value;
  68. double weight;
  69.  
  70. CoinSet(int[] num)
  71. {
  72. this.num = Arrays.copyOf(num, Coin.count);
  73. for (int i = 0; i < Coin.count; i++)
  74. {
  75. value += Coin.list[i].value * num[i];
  76. weight += Coin.list[i].weight * num[i];
  77. }
  78. }
  79.  
  80. @Override
  81. public String toString()
  82. {
  83. return String.format(" %10d : %6d %6d %6d %6d %6d %6d", value, num[0], num[1], num[2], num[3], num[4], num[5]);
  84. }
  85. }
  86.  
  87. static void n(double target, int[] num, int k, int value, double weight, ArrayList<CoinSet> resultSet)
  88. {
  89. if (epsilonEquals(weight, target, 0.001))
  90. {
  91. resultSet.add(new CoinSet(num));
  92. }
  93.  
  94. if (weight >= target) return;
  95. for (int i = k; i < Coin.count; i++)
  96. {
  97. num[i]++;
  98. n(target, num, i, value + Coin.list[i].value, weight + Coin.list[i].weight, resultSet);
  99. num[i]--;
  100. }
  101. }
  102.  
  103. static boolean epsilonEquals(double a, double b, double epsilon)
  104. {
  105. return Math.abs(a - b) <= epsilon;
  106. }
  107. }
  108.  
Success #stdin #stdout 0.27s 321152KB
stdin
23.7
23.7
23.7
23.7
23.7
23.7
23.7
23.7
23.7
23.7
stdout
Weight of Coins (gram) : 
Coin Weight    : 23.700000 g
Number of List : 23
amount(yen) :   1yen   5yen  10yen  50yen 100yen 500yen
---------------------------------------------------------
87,853,365ns

Weight of Coins (gram) : 
Coin Weight    : 23.700000 g
Number of List : 23
amount(yen) :   1yen   5yen  10yen  50yen 100yen 500yen
---------------------------------------------------------
239,627ns

Weight of Coins (gram) : 
Coin Weight    : 23.700000 g
Number of List : 23
amount(yen) :   1yen   5yen  10yen  50yen 100yen 500yen
---------------------------------------------------------
231,147ns

Weight of Coins (gram) : 
Coin Weight    : 23.700000 g
Number of List : 23
amount(yen) :   1yen   5yen  10yen  50yen 100yen 500yen
---------------------------------------------------------
240,121ns

Weight of Coins (gram) : 
Coin Weight    : 23.700000 g
Number of List : 23
amount(yen) :   1yen   5yen  10yen  50yen 100yen 500yen
---------------------------------------------------------
470,569ns

Weight of Coins (gram) : 
Coin Weight    : 23.700000 g
Number of List : 23
amount(yen) :   1yen   5yen  10yen  50yen 100yen 500yen
---------------------------------------------------------
256,251ns

Weight of Coins (gram) : 
Coin Weight    : 23.700000 g
Number of List : 23
amount(yen) :   1yen   5yen  10yen  50yen 100yen 500yen
---------------------------------------------------------
225,840ns

Weight of Coins (gram) : 
Coin Weight    : 23.700000 g
Number of List : 23
amount(yen) :   1yen   5yen  10yen  50yen 100yen 500yen
---------------------------------------------------------
231,529ns

Weight of Coins (gram) : 
Coin Weight    : 23.700000 g
Number of List : 23
amount(yen) :   1yen   5yen  10yen  50yen 100yen 500yen
---------------------------------------------------------
225,520ns

Weight of Coins (gram) : 
Coin Weight    : 23.700000 g
Number of List : 23
amount(yen) :   1yen   5yen  10yen  50yen 100yen 500yen
---------------------------------------------------------
1,775,710ns

Weight of Coins (gram) :