Sup极客们!
我正在尝试创建一个数组,列出数组元素总和的所有可能值。我相信这一定很容易,但我现在已经有2到3个小时了,而且我很沮丧,我想我差不多了......
var frootVals = [0,1,2,3,4,5]
var frootInc = frootVals
var fruitEnd = frootInc[frootInc.length-1]//begins at 5
var fruitAll = 15 // The total of all the numbers in the array. (this is actually
// calculated in another function, but lets just say I declared it as 15)
for (e = frootVals.length-2 ;fruitEnd !== fruitAll;e--){ //so I want it to
//finish when the final array entry is 15.
for (p = 1;p < e; p++){
var incEnd = frootInc[frootInc.length-p]
frootInc.push(incEnd + frootVals[p]) //1st time round (5 + 1 = 6, 5 + 2 = 7,
//5 + 3 =8, 5 + 4 = 9) THEN start again with 9 now being incEnd so pushes
//9+1 = 10 etc etc until the last digit is 15 and the whole loop stops...
}
}
编辑 - 基本上我所追求的最终结果是frootInc是一个整数数组[0,1,2,3,4,5,6,7,8,9,10,11,12, 13,14,15] - 我肯定我会放弃自己放弃但我只学了几个星期,所以这对你来说非常沉重。
答案 0 :(得分:0)
一个非常简单的解决方案是做这样的事情:
var frootVals = [0,1,2,3,4,5]
var result = [];
for (var i = 0; i < frootVals.length; i++){ // Iterate over the array twice
for (var j = 0; j < frootVals.length; j++){ // To calculate all combinations
result.push(frootVals[i] + frootVals[j]);
}
}
现在,如果您不想要重复,请尝试以下方法:
var frootVals = [0,1,2,3,4,5]
var result = [];
for (var i = 0; i < frootVals.length; i++){
for (var j = 0; j < frootVals.length; j++){
var value = frootVals[i] + frootVals[j];
if(result.indexOf(value) === -1){
result.push(value);
}
}
}
如果要输出排序结果,则可以使用result = result.sort()
。
答案 1 :(得分:0)
在考虑了一下你的问题之后,我认为最简单的解决方案是在递归到数组的最终值小于值的总和的情况下使用递归。
这是一个JS小提琴演示:http://jsfiddle.net/ukgzwpky/
要稍微分解一下(以便你可以确认我的问题是正确的:D),说我们有以下数组:
[0, 1, 2, 3, 10, 15, 30]
值的总和为:61
。所以预期的输出数组是:
[0, 1, 2, 3, 10, 15, 30, 31, 32, 33, 40, 45, 46, 47, 48, 55, 60, 61]
为了进一步细分,循环逻辑将执行以下操作:
// Get final value to add to previous values
var val = [0, 1, 2, 3, 10, 15, 30].pop();
// Add the final value - 30 - to all previous values (ignoring the zero)
.. loop here and push the values to our array ...
// For the first iteration, the updated array looks like:
[0, 1, 2, 3, 10, 15, 30, 31, 32, 33, 40, 45]
// New values calculated from: 30 + 1, 30 + 2, 30 + 3, 30 + 10, 30 + 15
此时,61
的最大值小于45
的最终值所以,我们再次执行此操作!
var val = [0, 1, 2, 3, 10, 15, 30, 31, 32, 33, 40, 45].pop();
.. loop here and push the values to our array ...
// Second iteration, the updated array looks like:
[0, 1, 2, 3, 10, 15, 30, 31, 32, 33, 40, 45, 46, 47, 48, 55, 60, 61]
// New values are: 45 + 1, 45 + 2, 45 + 3, 45 + 10, 45 + 15
// Note that 45 + 30 would be greater than our sum of 61, so we break
如果这是正确的,我写的这个脚本填充了这样一个数组:
function getPopulatedArray(arr) {
var max = arguments[1] || getSum(arr),
current = arr.pop(),
temp = [],
i = 1,
len = arr.length;
// Populate temp array with values
for (; i < len; i++) {
if ((arr[i] + current) < max) {
temp.push(arr[i] + current);
} else {
temp.push(max);
break;
}
}
arr.push(current);
arr = arr.concat(temp);
// Done? Or should we continue?
if (arr[arr.length - 1] < max) {
return getPopulatedArray(arr, max);
} else {
return arr;
}
}
再一次,JS小提示演示:http://jsfiddle.net/ukgzwpky/
希望这有帮助!