fork(1) 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. n(target, new int[Coin.count], 0, 0, 0, result);
  24. result.sort(Comparator.comparing(x -> x.value));
  25.  
  26. System.out.printf("%nCoin Weight : %f g%n", target);
  27. System.out.printf("Number of List : %d%n", result.size());
  28. System.out.printf("amount(yen) : 1yen 5yen 10yen 50yen 100yen 500yen%n");
  29. System.out.printf("---------------------------------------------------------%n");
  30. for(CoinSet set : result)
  31. {
  32. System.out.println(set);
  33. }
  34. System.out.println();
  35. }
  36.  
  37. static class Coin
  38. {
  39. static int count;
  40. static Coin[] list =
  41. {
  42. new Coin(1, 1.0D),
  43. new Coin(5, 3.7D),
  44. new Coin(10, 4.5D),
  45. new Coin(50, 4.0D),
  46. new Coin(100, 4.8D),
  47. new Coin(500, 7.0D),
  48. };
  49.  
  50. final int id;
  51. final int value;
  52. final double weight;
  53.  
  54. Coin(int value, double weight)
  55. {
  56. this.id = count++;
  57. this.value = value;
  58. this.weight = weight;
  59. }
  60. }
  61.  
  62. static class CoinSet
  63. {
  64. int[] num;
  65. int value;
  66. double weight;
  67.  
  68. CoinSet(int[] num)
  69. {
  70. this.num = Arrays.copyOf(num, Coin.count);
  71. for (int i = 0; i < Coin.count; i++)
  72. {
  73. value += Coin.list[i].value * num[i];
  74. weight += Coin.list[i].weight * num[i];
  75. }
  76. }
  77.  
  78. @Override
  79. public String toString()
  80. {
  81. return String.format(" %10d : %6d %6d %6d %6d %6d %6d", value, num[0], num[1], num[2], num[3], num[4], num[5]);
  82. }
  83. }
  84.  
  85. static void n(double target, int[] num, int k, int value, double weight, ArrayList<CoinSet> resultSet)
  86. {
  87. if (epsilonEquals(weight, target, 0.001))
  88. {
  89. resultSet.add(new CoinSet(num));
  90. }
  91.  
  92. if (weight >= target) return;
  93. for (int i = k; i < Coin.count; i++)
  94. {
  95. num[i]++;
  96. n(target, num, i, value + Coin.list[i].value, weight + Coin.list[i].weight, resultSet);
  97. num[i]--;
  98. }
  99. }
  100.  
  101. static boolean epsilonEquals(double a, double b, double epsilon)
  102. {
  103. return Math.abs(a - b) <= epsilon;
  104. }
  105. }
  106.  
Success #stdin #stdout 0.25s 321152KB
stdin
23.7
stdout
Weight of Coins (gram) : 
Coin Weight    : 23.700000 g
Number of List : 23
amount(yen) :   1yen   5yen  10yen  50yen 100yen 500yen
---------------------------------------------------------
         25 :     20      1      0      0      0      0
         36 :     11      1      2      0      0      0
         47 :      2      1      4      0      0      0
         71 :     16      1      0      1      0      0
         82 :      7      1      2      1      0      0
        117 :     12      1      0      2      0      0
        127 :      7      2      1      0      1      0
        128 :      3      1      2      2      0      0
        163 :      8      1      0      3      0      0
        173 :      3      2      1      1      1      0
        209 :      4      1      0      4      0      0
        218 :      3      3      0      0      2      0
        255 :      0      1      0      5      0      0
        410 :      0      0      1      0      4      0
        518 :     13      1      0      0      0      1
        529 :      4      1      2      0      0      1
        564 :      9      1      0      1      0      1
        575 :      0      1      2      1      0      1
        610 :      5      1      0      2      0      1
        620 :      0      2      1      0      1      1
        656 :      1      1      0      3      0      1
       1011 :      6      1      0      0      0      2
       1057 :      2      1      0      1      0      2

Weight of Coins (gram) :