teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

コメントがメソッドに付くように修正

2020/04/14 05:14

投稿

jimbe
jimbe

スコア13357

answer CHANGED
@@ -21,7 +21,6 @@
21
21
  public String toString() { return super.toString().replaceAll("_", " "); }
22
22
 
23
23
  /** 価値の高い順の Set を返す. */
24
- static private Set<Money> descValues = null;
25
24
  static Set<Money> descendingValues() {
26
25
  if(descValues == null) {
27
26
  descValues = new TreeSet<>(Comparator.comparing(Money::getValue).reversed()); //大きい順
@@ -29,6 +28,7 @@
29
28
  }
30
29
  return descValues;
31
30
  }
31
+ static private Set<Money> descValues = null;
32
32
  }
33
33
 
34
34
  public class Main {

1

Money の大きい順 Set 周りを変更

2020/04/14 05:14

投稿

jimbe
jimbe

スコア13357

answer CHANGED
@@ -21,17 +21,18 @@
21
21
  public String toString() { return super.toString().replaceAll("_", " "); }
22
22
 
23
23
  /** 価値の高い順の Set を返す. */
24
+ static private Set<Money> descValues = null;
24
- static Set<Money> getDescendingSet() {
25
+ static Set<Money> descendingValues() {
26
+ if(descValues == null) {
25
- Set<Money> result = new TreeSet<>(Comparator.comparing(Money::getValue).reversed()); //大きい順
27
+ descValues = new TreeSet<>(Comparator.comparing(Money::getValue).reversed()); //大きい順
26
- result.addAll(Arrays.asList(values()));
28
+ descValues.addAll(Arrays.asList(values()));
29
+ }
27
- return result;
30
+ return descValues;
28
31
  }
29
32
  }
30
33
 
31
34
  public class Main {
32
35
  public static void main(String[] args) throws IOException {
33
- Set<Money> moneys = Money.getDescendingSet();
34
-
35
36
  try (InputStreamReader reader = new InputStreamReader(System.in);
36
37
  BufferedReader in = new BufferedReader(reader); ) {
37
38
  for(String line; (line = in.readLine()) != null; ) {
@@ -46,15 +47,15 @@
46
47
  } else if(change.signum() == 0) {
47
48
  System.out.println("ZERO");
48
49
  } else {
49
- print(prepare(change, moneys));
50
+ print(prepare(change));
50
51
  }
51
52
  }
52
53
  }
53
54
  }
54
- /** change 分の紙幣/硬貨を用意<br>(moneys は大きい順にソートされている必要がある) */
55
+ /** change 分の紙幣/硬貨を用意 */
55
- private static Map<Money,BigDecimal> prepare(BigDecimal change, Set<Money> moneys) {
56
+ private static Map<Money,BigDecimal> prepare(BigDecimal change) {
56
57
  Map<Money,BigDecimal> result = new HashMap<>();
57
- for(Money m : moneys) {
58
+ for(Money m : Money.descendingValues()) {
58
59
  if(change.compareTo(m.getValue()) >= 0) {
59
60
  BigDecimal[] dr = change.divideAndRemainder(m.getValue()); //[0]=商,[1]=剰余
60
61
  result.put(m, dr[0]);