lua 贪心算法(找钱算法)

function GetChipsFromNum(num)
    if not num then return end
    gLog("--- 开始找钱")
    local chipType = {1, 2, 5, 10, 50, 100, 500, 1000, 5000}
    -- 排序
    table.sort(chipType, function(a, b)
        return a > b
    end)
    local changeTbl = {}
    for k,value in pairs(chipType) do
        local zheng_shu = math.floor(num/value)
        local yu_shu = num%value
        if zheng_shu ~= 0 then
            num = yu_shu
            table.insert(changeTbl, {chipsType = value, num = zheng_shu})
            gLog("币种:{2}-- 数量:{0} --- 余数:{1}", zheng_shu, yu_shu, value)
        end
    end
    return changeTbl
end