Javascript 对数组值求和,直到达到特定值,然后返回使用过的值
是否有一个函数可以接受一个数字数组和一个特定的数字,并在数组中添加一些数字,以获得特定的数字,并在数组中返回您所给出的数字 这方面的一个例子是:Javascript 对数组值求和,直到达到特定值,然后返回使用过的值,javascript,arrays,subset,subset-sum,Javascript,Arrays,Subset,Subset Sum,是否有一个函数可以接受一个数字数组和一个特定的数字,并在数组中添加一些数字,以获得特定的数字,并在数组中返回您所给出的数字 这方面的一个例子是: var specificFunction = function(list, target){ // some kind of code here that adds the numbers in the array till it gets the number you want and returns in a array the numbe
var specificFunction = function(list, target){
// some kind of code here that adds the numbers in the array till it gets the number you want and returns in a array the numbers it used to get the number you want
};
specificFunction([256,8,512,8192,32,1024,16,2], 9224); // [8192,1024,8]
如果没有这样一个函数,那么我将如何制作一个呢
如果有这样一个函数,那么我在哪里可以找到它,或者它是内置的?Google for Subset sum problem。这是我在数小时的搜索后第一次看到这个函数,并回答了我的问题,但我还没有看到一个javascript实现,如果您能提供一个指向其javascript实现示例的链接,我将不胜感激这是我通过谷歌搜索得到的。谢谢@thefourtheye如果你想把这个作为答案,我会接受的,因为你已经完全回答了我的问题。好的:-)试着自己实现这个算法,它会很有趣。