Skip to content

Instantly share code, notes, and snippets.

@TooBug
Created May 31, 2017 02:17
Show Gist options
  • Save TooBug/9781db6bc14f0d353c4dc95bbb71d0e2 to your computer and use it in GitHub Desktop.
Save TooBug/9781db6bc14f0d353c4dc95bbb71d0e2 to your computer and use it in GitHub Desktop.
给定指定数和一个数组组合,求合为指定数的最少次数组合
var ret = [];
var levels = [698, 208, 128, 68, 6];
function getRet(sum, level){debugger;
if(!level) level = 0;
if(level > levels.length - 1) return false;
var thisCount = Math.floor(sum / levels[level]);
while(thisCount >= 0){
ret[level] = thisCount;
var nextSum = sum - thisCount*levels[level];
var ok;
if(nextSum){
ok = getRet(nextSum, level+1);
}else{ok = true;}
if(!ok){
thisCount--;
}else{
return ret;
}
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment