Comment detail

コインを減らす払い方 (Nested Flatten)

	
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
| 支払額 手持ち 支払 残高 |
支払額 := 147.
手持ち := 支払 := {1->2. 10->4. 100->3}
   inject: Bag new
   into: [:bag :assc | bag add: assc key withOccurrences: assc value; yourself].

残高 := 手持ち sum - 支払額.
#(500 100 50 10 5 1) do: [:コイン |
   | 枚数 |
   (枚数 := 残高 // コイン) > 0 ifTrue: [
      残高 := 残高 \\ (コイン * 枚数).
      枚数 timesRepeat: [支払 remove: コイン ifAbsent: []]]].
^支払 sortedElements asArray

"=> {1->2 . 100->2} "

Index

Feed

Other

Link

Pathtraq

loading...