11=>[5,3,1]
输出[5,5,1]
7
输出[5,1,1]
const getMax=(arr)=>{
return Math.max.apply(null,arr)
}
function getCoins(arr,number){
const max=getMax(arr);
const n=parseInt(number/max);
const m=number%max;
let newArr=[];
if(n>0){
newArr=new Array(n).fill(max)
}
if(m>0){
let [max,...otherArr]=arr;
newArr=newArr.concat(...getCoins(otherArr,m))
}
return newArr
}
原文:https://www.cnblogs.com/yangwang12345/p/14638233.html