[英]How to get all possible combinations of an integer array items?
I have an array of numbers.我有一个数字数组。 In this array each number is repeating for "r" times.
在这个数组中,每个数字都重复了“r”次。
This function is generating the array:此函数正在生成数组:
var n = 8;
var k = 4;
var r = 3;
var comb = n * r / k;
function getNumbers() {
var array = new Array();
for (i = 0; i < r; i++) {
for (j = 0; j < n; j++) {
array.push(j);
}
}
return array;
}
The numbers from this array I want to split them in "comb=n*r/k" uniq small arrays with the length of k;这个数组中的数字我想将它们拆分为长度为 k 的“comb=n*r/k”uniq 小数组;
for this I made the following function:为此,我做了以下功能:
function InsertNumber(array) {
var finalarray = GetArrays(comb);
for (j = 0; j < array.length; j++) {
for (i = 0; i < finalarray.length; i++) {
if (ContainX(array[j], finalarray[i])) {
if (finalarray[i].length <= k) {
finalarray[i].push(array[j]);
Console.log(array[j]);
var index = array.indexOf(array[j]);
array.splice(index, 1);
InserNumber(array);
}
}
}
}
ShowTable(finalarray);
}
function GetArrays(x) {
var array = new Array();
for (i = 0; i < x; i++) {
var smallArray= new Array();
array.push(smallArray);
}
return array;
}
function ContainX(array,element) {
var result = false;
for (i = 0; i < array.length; i++) {
if (element === array[i]) {
result = true;
}
}
return result;
}
finaly I want to display all the small arays items in a table using this function:最后我想使用这个函数在一个表中显示所有的小 arays 项目:
function ShowTable(array) {
document.write("<table>")
for (i = 0; i < array.lenght; i++) {
document.write("<tr>")
for (j = 0; j < array[i].legth; j++) {
document.write("<td>" + array[i][j] + "</td>")
}
document.write("</tr>")
}
document.write("</table>")
}
I think that the step by step algorithm to get the expected result may be ok, but I am not able to see the result in browser because of the recursive function InsertNumber().我认为获得预期结果的逐步算法可能没问题,但是由于递归函数InsertNumber(),我无法在浏览器中看到结果。
Can you help me with a better method to generate all combinations of all numbers in an array, where the the array items may repeat for r times?你能帮我用更好的方法来生成数组中所有数字的所有组合,其中数组项可能重复 r 次吗?
I am open for any solution which can fix my issue.我愿意接受任何可以解决我的问题的解决方案。
Edit:编辑:
Exemple: mainArray=[0,0,1,1,2,2]
;例如:
mainArray=[0,0,1,1,2,2]
;
I want to split this array in:我想将此数组拆分为:
arr1=[0,1];
arr2=[0,2];
arr3=[1,2];
this 3 arrays are containing all items of mainArray and are uniq!!!.
In this exemple: n=3, k=2, r=2, comb=n*r/k=3;
n=total unique numbers from `mainArray [0,1,2]`;
k=length of small arrays
r= number of repetition of each unique n;
comb= the number of small arrays;
**Edit2- Exemple2:**
mainArray=[0,0,1,1,2,2,3,3,4,4]
arr1=[0,1];
arr2=[0,2];
arr3=[1,3];
arr4=[2,4];
arr5=[3,4];
n=5, unique numbers in main array;
k=2, length of small array;
r=2, repetition of each number in main array;
comb=5*2/2=number of combinations which must result!.
(all the items from mainArray are spllited in small arr is such a way to not have small array with same items) (来自 mainArray 的所有项目都在小 arr 中拆分是这样一种方式,即没有具有相同项目的小数组)
This proposal works with an array and a given length
for the part array.这个提议适用于一个数组和一个给定
length
的部分数组。
Distribution of values with
length = 2
length = 2
的值分布0 0 1 1 2 2 3 3 4 4 0 1 0 2 1 3 2 4 3 4
Distribution of values with
length = 3
length = 3
的值分布0 0 0 1 1 1 2 2 2 3 3 3 4 4 4 5 5 5 6 6 6 7 7 7 0 1 2 0 1 3 0 2 4 1 3 5 2 4 6 3 5 7 4 6 7 5 6 7
The key feature is to splice the used value and increase the position with length - 1
for the next item to push to the part array.关键特征是拼接使用的值并增加
length - 1
为length - 1
的位置,以便下一个项目推送到零件数组。 If the position p
is outside of the array, then the position is set to the last element of the array.如果位置
p
在数组之外,则将位置设置为数组的最后一个元素。
function getParts(array, length) { var r = [], values, i, p; while (array.length > length) { values = []; p = 0; for (i = 0; i < length; i++) { if (p >= array.length) { p = array.length - 1; } values.push(array.splice(p, 1)[0]); p += length - 1; } r.push(values); } r.push(array); return r; } function print(o) { document.write('<pre>' + JSON.stringify(o, 0, 4) + '</pre><hr>'); } print(getParts([0, 0, 1, 1, 2, 2], 2)); print(getParts([0, 0, 1, 1, 2, 2, 3, 3, 4, 4], 2)); print(getParts([0, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 7, 7, 7], 3));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.