>>281 Ruby ぴったり払えない時は No Solutions と出力
currency = [1, 5, 10, 50, 100, 500, 1000, 2000, 5000, 10000]
sums = currency.size.times.map { |i| currency[0..i].sum }
[74664, 55998, 37332, 500, 340, 300, 197, 161, 157, 60, 40].each { |yen|
print '%d => ' % yen
min = currency.zip(sums).reverse_each.map { |c, s|
yen -= c * x = [yen/c, -(-yen/s)].min
x
}.max
puts yen.zero? ? min : 'No Solutions'
}
#=>
74664 => 4
55998 => 3
37332 => 2
500 => 1
340 => 3
300 => 2
197 => 3
161 => 1
157 => 2
60 => 1
40 => 3
currency = [1, 5, 10, 50, 100, 500, 1000, 2000, 5000, 10000]
sums = currency.size.times.map { |i| currency[0..i].sum }
[74664, 55998, 37332, 500, 340, 300, 197, 161, 157, 60, 40].each { |yen|
print '%d => ' % yen
min = currency.zip(sums).reverse_each.map { |c, s|
yen -= c * x = [yen/c, -(-yen/s)].min
x
}.max
puts yen.zero? ? min : 'No Solutions'
}
#=>
74664 => 4
55998 => 3
37332 => 2
500 => 1
340 => 3
300 => 2
197 => 3
161 => 1
157 => 2
60 => 1
40 => 3