let num = [2,3,5,7,9]
let result = []
let count = 0;
//js 求和版
const calc=(res)=>{
for(let i = 0;i<num.length;i++){
res.push(num[i]);
let sum = res.reduce((preValue,curValue)=>preValue + curValue);
if(sum===13){
console.log("当前组合为",res);
res.pop();
count++;
return
}else if(sum < 13){
calc(res)
}
res.pop();
}
}
calc(result)
console.log(count)
//js 求差版
/*
const num = [2, 3, 5, 7, 9]
let count = 0;
const getCombination = (target, arr = [])=>{
num.forEach(value => {
if (target - value < 0) {
return
}else if (target - value === 0) {
// console.log(arr.concat(value))
console.log(arr.concat(value))
count++;
return
}
getCombination(target - value, arr.concat(value))
})
}
getCombination(13)
console.log(count)
*/
//py版--https://www.v2ex.com/t/719057?p=1 中的#46楼
/*
num = [2,3,5,7,9]
result = []
count = 0;
def calc(result):
for i in range(0,5):
result.append(num[i])
if sum(result) == 13:
print(result)
global count;
count = count+1;
result.pop()
return
elif sum(result) < 13:
calc(result)
result.pop()
return
calc(result)
print(count)
*/
console