提領鈔券時可以選擇欲提領組合。我今天想開台coding會有搞頭嗎?
受到記帳本(function countCoin)的啟發,當使用者要提領的時候也可以讓使用者選擇要如何提領。

如上:輸入一個金額要把所有可能的鈔券組合打印出來。當然也可以導入50 ,10 ,5 ,1 元。
但是log會太長,數字太大log也會太長。javaScript的部分就自己搭配html標籤跟jquery抓取input值吧。
接下來問各位,你們覺得coding的盡頭會是什麼?
各位又是為了什麼學coding?
當上工程師達成階段性目標。然後呢?
上面的問題可以不用回答,但後面的問題希望可以回答我。
也希望大家能踴躍更我討論『人生』,如果有故事的可以私下找我。我聽你講。
#python3
def cashCount(money_box, box_index, cash):
if box_index == len(genre) - 1:
money_box[box_index] = cash // genre[box_index]
ans.append([i for i in money_box])
return
if box_index != len(genre) - 1:
for i in range(cash // (genre[box_index]) + 1):
money_box[box_index] = i
cashCount(money_box, box_index + 1, cash - genre[box_index] * i)
box_index += 1
return
if __name__ == "__main__":
input_cash = int(input("請輸入金額:"))
print("輸入金額:%d" % input_cash)
ans = []
genre = [1000, 500, 100]
cashCount([0 for i in range(len(genre))], 0, input_cash)
count = 0
for cash_combo in ans:
coin_str = ""
count += 1
for i in range(len(genre)):
coin_str += "%d元%d張," % (genre[i], cash_combo[i])
print(str(count) + "." + coin_str + "零錢:%d元" % (input_cash % genre[len(genre) - 1]))
#JavaScript
function cashCount(money_box, box_index, cash){
if (box_index === (genre_len)-1){
money_box[box_index] = Math.floor(cash/genre[box_index])
var box = []
money_box.forEach(x => box.push(x))
ans.push(box)
}
if (box_index !== (genre_len)-1){
for(var i=0; i<Math.floor(cash/genre[box_index])+1; i++){
money_box[box_index] = i
cashCount(money_box, box_index + 1, cash - genre[box_index] * i)
}
box_index++
}
}
const input_cash = 1840
const genre = [1000, 500, 100]
const ans = []
const cash_box = []
const genre_len = genre.length
for (var i=0; i< genre_len; i++){
cash_box.push(0)
}
cashCount(cash_box, 0, input_cash)
console.log(ans)
字串就自己拼吧!懶得寫了。
#GO
package main
import (
"fmt"
"strconv"
)
var genre = []int{1000, 500, 100}
var ans [][]int
var inputCash int
func cashCount(moneyBox []int, boxIndex int, cash int) {
if boxIndex == len(genre)-1 {
moneyBox[boxIndex] = cash / genre[boxIndex]
var box []int
for _, v := range moneyBox {
box = append(box, v)
}
ans = append(ans, box)
return
}
if boxIndex != len(genre)-1 {
for i := 0; i <= (cash / genre[boxIndex]); i++ {
moneyBox[boxIndex] = i
cashCount(moneyBox, boxIndex+1, cash-genre[boxIndex]*i)
}
boxIndex++
return
}
}
func main() {
fmt.Println("請輸入金額:")
fmt.Scan(&inputCash)
fmt.Println(inputCash)
var cashBox []int
count := 0
for i := 0; i < len(genre); i++ {
cashBox = append(cashBox, 0)
}
cashCount(cashBox, 0, inputCash)
for _, cashCombo := range ans {
coinStr := ""
count++
for i := 0; i < len(genre); i++ {
genreValue := strconv.Itoa(genre[i])
cashComboValue := strconv.Itoa(cashCombo[i])
genreStr := genreValue + "元" + cashComboValue + "張,"
coinStr += genreStr
}
fmt.Printf("%d.%s零錢:%d元\n", count, coinStr, inputCash%genre[len(genre)-1])
}
}