You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
lavos/cap3/ex15.go

24 lines
509 B

package cap3
var (
cashes = []int{50, 20, 10, 5, 2, 1}
)
// Ex15 最少的纸币找零钱
func Ex15(amount, paid int) map[int]int {
result := map[int]int{}
// 还要找的钱
change := paid - amount
for i, cash := range cashes {
// 当前面额需要找的张数,整除后结果为0说明当前面额不足以找零
cashNum := change / cash
if cashNum != 0 {
result[i] = cashNum
// 当前面额找完后更新剩余未找余额
change = change - cashNum*cash
}
}
return result
}