35 选 7 且和值为 100 引发的血案

题目意思:M个数的列表中选N个数,且这N个数的和为指定值,并指定组合的和值允许的偏差上限。
fx(列表,和值,偏差,个数)
以下代码请在老司机的抚摸下操作,如抛锚概不负责:

let
fx=(a,b,c,d)=>Text.Split(Web.Page("<script>function getCombBySum(array,sum,tolerance,targetCount){
var util = {getCombination: function(arr, num) {var r=[];
(function f(t,a,n){if (n==0){return r.push(t);}
for (var i=0,l=a.length; i<=l-n; i++)
{f(t.concat(a[i]), a.slice(i+1), n-1);}})([],arr,num);return r;},
getArrayIndex: function(array) {var i = 0,r = [];for(i = 0;i<array.length;i++){r.push(i);}
return r;}},logic = {
init: function(array,sum) {var _array = array.concat(),r = [],i = 0;_array.sort(function(a,b){return a - b;});
for(i = 0;i<_array.length;i++){if(_array[i]<=sum){r.push(_array[i]);}else{break;}}return r;},
core: function(array,sum,arrayIndex,count,r){var i = 0,k = 0,combArray = [],_sum = 0,_cca = [],_cache = [];if(count == _returnMark){return;}
combArray = util.getCombination(arrayIndex,count);
for(i = 0;i<combArray.length;i++){_cca = combArray[i];_sum = 0;_cache = [];
for(k = 0;k<_cca.length;k++){_sum += array[_cca[k]];_cache.push(array[_cca[k]]);}
if(Math.abs(_sum-sum) <= _tolerance){r.push(_cache);}}
logic.core(array,sum,arrayIndex,count-1,r);}},
r = [],_array = [],_targetCount = 0,_tolerance = 0,_returnMark = 0;
_targetCount = targetCount || _targetCount;
_tolerance = tolerance || _tolerance;
_array = logic.init(array,sum);if(_targetCount){_returnMark = _targetCount-1;}
logic.core(_array,sum,util.getArrayIndex(_array),(_targetCount || _array.length),r);return r;}
document.write(getCombBySum(["&Text.Combine(List.Transform(a,Text.From),",")&"],"&Text.From(b)&","&Text.From(c)&","&Text.From(d)&").join('|'));
</script>")[Data]{0}[Children]{0}[Children]{1}[Text]{0},"|")
in
fx

file

编程其实没那么难,作为大自然的搬运工,我只是用了CTRL+C/V而已。
file

道高一尺 魔高一丈
https://pbihub.cn/users/44
M与DAX的恩怨纠葛