Javascript 如何创建排列以达到目标编号,但重用提供的编号?

Javascript 如何创建排列以达到目标编号,但重用提供的编号?,javascript,math,recursion,integer,Javascript,Math,Recursion,Integer,编辑: var Combos = [1, 2, 4], Target = 4; for(var i = 0; i < Combos.length; i++){ var Available = []; for(var j = -1; j < i; j++) Available.push(Combos[j+1]); for(var k = 0; k < Available.length; k++){

编辑:

var Combos = [1, 2, 4],
    Target = 4;

    for(var i = 0; i < Combos.length; i++){
        var Available = [];

        for(var j = -1; j < i; j++) Available.push(Combos[j+1]);
        for(var k = 0; k < Available.length; k++){
            var C = 0;
            var Att = 0;

            while(C < Target){ C += Available[k]; Att++; }
            if(C === Target) console.log(new Int8Array(Att).fill(Available[k]));
        }
    }
[1, 1, 1, 1]
[1, 1, 1, 1]
[2, 2]
[1, 1, 1, 1]
[2, 2]
[4]
我不知道为什么我会重复1,1,1,1和2,2,但我正在调查


我提供的数字数组将是[1,2,4],我试图找到一个始终合适的目标;让我们假设这是4

我如何返回一个组合数组,其中流程重用提供的数字,例如:我在这里看到的所有问题只使用一次数字,因此这些程序将返回
[[4]]
,而我希望结果是
[[1,1,1,1],[1,1,2],[2,2],[4]

首先,我考虑使用模来表示:目标是整数吗?如果是,我们可以立即返回target/1。但我真的搞不懂逻辑

我与IRC聊天,也许这有助于您理解:

13:20   Robinlemon  So I have the numbers [1, 2] and I'm trying to make 4 so I want a function that will return [[1, 1, 1, 1], [1, 1 ,2], [2, 2], [4]] -> all the possible combinations to make the target, by reusing the numbers supplied
13:21   kakashiAL   how does your function api looks like?
13:21   Robinlemon  What do you mean?
13:21   Robinlemon  I've scrapped everything
13:21   kakashiAL   foo(myArray, combination=
13:21   Robinlemon  Because I can't ever work out the logic to do this
13:21   Robinlemon  Well I suppose i'd have
13:22   Robinlemon  Permutate(Price) => SubPermutate()
13:22   Robinlemon  Then return
13:22   Robinlemon  and tierate through each sub function
13:22   kakashiAL   so you have [1, 2] and you want to make 4, which means you want to have this:
13:22   kakashiAL   1, 1, 1, 1
13:22   kakashiAL   2, 2, 2, 2
13:23   kakashiAL   1, 2, 2, 2
13:23   kakashiAL   1, 1, 2, 2
13:23   kakashiAL   and so on
13:23   kakashiAL   right?
13:26   Robinlemon  yep
13:26   Robinlemon  well no
13:26   Robinlemon  The numbers need to add to 4
13:26   Robinlemon  so it wouldnt be 1, 1, 2, 2
13:26   Robinlemon  it would be 1 1 2
13:26   Robinlemon  get it
13:26   kakashiAL   you have the numbers 1, 2 and 4, correct?
13:28   Robinlemon  yes
13:28   Robinlemon  For example sake
13:29   Robinlemon  It should work with anything tho
13:29   Robinlemon  The target will always be reachable with the numbers
13:29   kakashiAL   okay, now you want to get this, with 1, 2 and 4:
13:29   kakashiAL   1, 1, 1
13:29   kakashiAL   2, 2, 2
13:29   kakashiAL   4, 4, 4
13:29   kakashiAL   1, 2, 4
13:29   kakashiAL   1, 1, 2
13:29   Robinlemon  No
13:29   Robinlemon  1, 1, 1, 1
13:29   Robinlemon  2, 2
13:29   Robinlemon  4
13:30   Robinlemon  The numbers need to add to 4
13:30   Robinlemon  I wnat the combinations that add to 4
13:30   kakashiAL   ahh if you have 6 you would have this:
13:30   kakashiAL   1, 1, 1, 1, 1, 1
13:30   kakashiAL   2, 2, 2
13:30   Robinlemon  Yes

谢谢阅读,期待您的回复

您可以使用带有一些退出条件的递归方法来检查总和或索引

否则,获取实际元素并将其连接到临时对象,然后再次调用该函数,而不增加索引

另一个方向是使用递增的索引再次调用函数

函数getCombinations(数组、和){ 功能叉(i,t){ var s=t.reduce(函数(a,b){返回a+b;},0); 如果(总和=s){ 结果:推(t); 返回; } if(s>sum | | i==array.length){ 返回; } fork(i,t.concat([array[i]]); 叉(i+1,t); } var结果=[]; fork(0,[]); 返回结果; } log(getcompositions([1,2,4],4)); log(getcompositions([0.11,0.33,1],26.66)); log(getcompositions([0.11,0.33,1],23.33))
.as console wrapper{max height:100%!important;top:0;}
这是一个典型的动态编程案例问题,与其他方法相比,它很可能是一种更有效的解决方法。这是一个非递归的动态规划解决方案

函数getCombos(a,t){ var h={}, len=a.长度, n=0; 对于(变量i=0;is.concat(n)); } } 返回h[t]| |[]; } var结果=[]; 控制台时间(“从[1,2,4]到总和4的dp”); 结果=getCombos([1,2,4],4); 控制台.TIMEND(“1,2,4]的dp总计4”); log(JSON.stringify(result)); 控制台时间(“1,5,9]的dp总计500”); 结果=getCombos([1,5,9],500); 控制台时间结束(“1,5,9]的dp总计500”);
log(“有”,result.length,“解决方案”)不错,你试过什么吗?Stackoverflow不是免费的代码编写或教程服务。请尝试一些东西,当你的代码遇到实际问题时,回来问问题,然后展示你所尝试的。看,我已经尝试过了,我甚至说我已经尝试过使用模,但是,我需要它通过一个函数动态生成if,然后返回,所以在这一点上它很愚蠢,我认为有一个更简单的方法来做!发布一些代码。我们不是来阅读您的聊天日志的。我已经添加了一些代码,我仍在努力解决这个问题,非常感谢您的帮助!哇,谢谢!这里的问题是数字1、2和4是示例,我的实际数字是[1、0.33、0.11],目标是26.66、23.33等等,所以是否可以调整它并使其仍然有效?您可以按升序排列项目。但是使用浮动时,您可能会出现一些不必要的行为,比如您刚刚在内部fork中添加了+1?>叉(i+1,t.concat([array[i]]);是的,没有更多的改变,它可以实现为标志。这听起来可能有点愚蠢的请求,但目前我得到较低的组合,例如[1,1,1,1],然后[4],我通常只是.reverse()和sue第一个索引,但因为我的组合太大,我的内存不足,我如何调整它以使其向后工作,这样我就不需要反转,然后返回第一个组合,显然,如果不向后工作,您只会得到第一个组合并返回-这将是毫无意义的。我想我们不是用fork(0,[]),而是用fork(array.length,[]),然后是I-1?天哪,老兄,你真的去城里了!谢谢,我一定会用这个!有没有简单的方法可以使它成为单层的,即不重复使用数组中的数字?@Robinlemon,请添加一个例子。@Robinlemon,你的意思是不重复使用一个项目,而是所有项目的组合。这就产生了所有子阵列的问题。我们应该这样做。如中所示,当前系统要达到4的值[1,2,4],将输出[1,1,1,1]、[1,1,2]、[2,2]、[4];然而,如果每个数字只能使用一次,那么它只能是[[4]]。