Created
May 31, 2017 02:17
-
-
Save TooBug/9781db6bc14f0d353c4dc95bbb71d0e2 to your computer and use it in GitHub Desktop.
给定指定数和一个数组组合,求合为指定数的最少次数组合
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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