我想生成一个包含时间序列所有排列的数组。假设数字可以是0,5,10,25,第一个排列是[0,0,0,0,0,0,0]。下一个排列可以是[0,0,0,0,0,0,5],依此类推,直到[25,25,25,25,25,25,25]。在这种情况下应该有4 ^ 6 = 4096个排列因为有4个数字和7个时隙。
有人可以帮我理解如何开始解决这个问题吗?我想用javascript写这个。谢谢你的考虑。
答案 0 :(得分:1)
请参阅附加的脚本,我刚刚放在一起。它适用于您的情况。我限制为4种排列,但应该很容易扩展到7.我希望你能看到这种模式。
var array = new Array();
var values = [0,5,10,25];
for(var i = 0; i < Math.pow(4,4); i++) {
// calculate which indexes to retrieve value from loops through 1..4
var entry = [
Math.floor(i / Math.pow(4,0)) % 4, // increment this with every i
Math.floor(i / Math.pow(4,1)) % 4, // increment this with every 4 * i
Math.floor(i / Math.pow(4,2)) % 4, // increment this with every 16 * i
Math.floor(i / Math.pow(4,3)) % 4 // increment this with every 64 * i etc
];
array.push([values[entry[0]], values[entry[1]], values[entry[2]],values[entry[3]]]);
}
document.write(JSON.stringify(array));
&#13;
答案 1 :(得分:0)
下面,我创建了一个循环来生成所有排列。这是一个迭代的解决方案。
更新:我已将代码缩减为以下内容:
function permutations(series, size) {
return zeroFill(Math.pow(series.length,size)).map(function(r,i) {
return zeroFill(size).map(function(c,j) {
return series[Math.floor(i/Math.pow(series.length,j))%series.length];
}).reverse();
});
}
function zeroFill(n) {
return new Array(n+1).join('0').split('');
}
由于这是一个内存密集型流程,我从列表中删除了25
,并将每个已处理的条目缩减为5
的长度。
var out = document.getElementById('output');
var permutations = generate([0, 5, 10], 5);
printResults(permutations);
// Generate permutations.
function generate(series, size) {
var result = [];
var len = series.length;
var limit = Math.pow(len, size);
for (var i = 0; i < limit; i++) {
var entry = [];
for (var j = 0; j < size; j++) {
entry[size-j] = series[Math.floor(i / Math.pow(len, j)) % len];
}
result.push(entry);
}
return result;
}
// Utility Functions - These are for display purposes.
function printResults(list) {
var width = ('' + list.length).length;
list.forEach(function(list, index) {
print('[' + FormatNumberLength(index, width) + '] ' + numberJoin(list, 3));
});
}
function print(text) {
out.innerHTML += text + '<br />';
}
function numberJoin(intList, width) {
return intList.map(function(value) {
return FormatNumberLength(value, width);
}).join(' ');
}
function FormatNumberLength(num, length, useZero) {
var c = useZero ? '0' : ' ';
var r = "" + num;
while (r.length < length) r = c + r;
return r;
}
&#13;
#output {
font-family: monospace;
white-space: pre;
}
&#13;
<div id="output"></div>
&#13;
预期结果
[ 0] 0 0 0 0 0
[ 1] 0 0 0 0 5
[ 2] 0 0 0 0 10
[ 3] 0 0 0 5 0
[ 4] 0 0 0 5 5
[ 5] 0 0 0 5 10
[ 6] 0 0 0 10 0
[ 7] 0 0 0 10 5
[ 8] 0 0 0 10 10
[ 9] 0 0 5 0 0
[ 10] 0 0 5 0 5
[ 11] 0 0 5 0 10
[ 12] 0 0 5 5 0
[ 13] 0 0 5 5 5
[ 14] 0 0 5 5 10
[ 15] 0 0 5 10 0
[ 16] 0 0 5 10 5
[ 17] 0 0 5 10 10
[ 18] 0 0 10 0 0
[ 19] 0 0 10 0 5
[ 20] 0 0 10 0 10
[ 21] 0 0 10 5 0
[ 22] 0 0 10 5 5
[ 23] 0 0 10 5 10
[ 24] 0 0 10 10 0
[ 25] 0 0 10 10 5
[ 26] 0 0 10 10 10
[ 27] 0 5 0 0 0
[ 28] 0 5 0 0 5
[ 29] 0 5 0 0 10
[ 30] 0 5 0 5 0
[ 31] 0 5 0 5 5
[ 32] 0 5 0 5 10
[ 33] 0 5 0 10 0
[ 34] 0 5 0 10 5
[ 35] 0 5 0 10 10
[ 36] 0 5 5 0 0
[ 37] 0 5 5 0 5
[ 38] 0 5 5 0 10
[ 39] 0 5 5 5 0
[ 40] 0 5 5 5 5
[ 41] 0 5 5 5 10
[ 42] 0 5 5 10 0
[ 43] 0 5 5 10 5
[ 44] 0 5 5 10 10
[ 45] 0 5 10 0 0
[ 46] 0 5 10 0 5
[ 47] 0 5 10 0 10
[ 48] 0 5 10 5 0
[ 49] 0 5 10 5 5
[ 50] 0 5 10 5 10
[ 51] 0 5 10 10 0
[ 52] 0 5 10 10 5
[ 53] 0 5 10 10 10
[ 54] 0 10 0 0 0
[ 55] 0 10 0 0 5
[ 56] 0 10 0 0 10
[ 57] 0 10 0 5 0
[ 58] 0 10 0 5 5
[ 59] 0 10 0 5 10
[ 60] 0 10 0 10 0
[ 61] 0 10 0 10 5
[ 62] 0 10 0 10 10
[ 63] 0 10 5 0 0
[ 64] 0 10 5 0 5
[ 65] 0 10 5 0 10
[ 66] 0 10 5 5 0
[ 67] 0 10 5 5 5
[ 68] 0 10 5 5 10
[ 69] 0 10 5 10 0
[ 70] 0 10 5 10 5
[ 71] 0 10 5 10 10
[ 72] 0 10 10 0 0
[ 73] 0 10 10 0 5
[ 74] 0 10 10 0 10
[ 75] 0 10 10 5 0
[ 76] 0 10 10 5 5
[ 77] 0 10 10 5 10
[ 78] 0 10 10 10 0
[ 79] 0 10 10 10 5
[ 80] 0 10 10 10 10
[ 81] 5 0 0 0 0
[ 82] 5 0 0 0 5
[ 83] 5 0 0 0 10
[ 84] 5 0 0 5 0
[ 85] 5 0 0 5 5
[ 86] 5 0 0 5 10
[ 87] 5 0 0 10 0
[ 88] 5 0 0 10 5
[ 89] 5 0 0 10 10
[ 90] 5 0 5 0 0
[ 91] 5 0 5 0 5
[ 92] 5 0 5 0 10
[ 93] 5 0 5 5 0
[ 94] 5 0 5 5 5
[ 95] 5 0 5 5 10
[ 96] 5 0 5 10 0
[ 97] 5 0 5 10 5
[ 98] 5 0 5 10 10
[ 99] 5 0 10 0 0
[100] 5 0 10 0 5
[101] 5 0 10 0 10
[102] 5 0 10 5 0
[103] 5 0 10 5 5
[104] 5 0 10 5 10
[105] 5 0 10 10 0
[106] 5 0 10 10 5
[107] 5 0 10 10 10
[108] 5 5 0 0 0
[109] 5 5 0 0 5
[110] 5 5 0 0 10
[111] 5 5 0 5 0
[112] 5 5 0 5 5
[113] 5 5 0 5 10
[114] 5 5 0 10 0
[115] 5 5 0 10 5
[116] 5 5 0 10 10
[117] 5 5 5 0 0
[118] 5 5 5 0 5
[119] 5 5 5 0 10
[120] 5 5 5 5 0
[121] 5 5 5 5 5
[122] 5 5 5 5 10
[123] 5 5 5 10 0
[124] 5 5 5 10 5
[125] 5 5 5 10 10
[126] 5 5 10 0 0
[127] 5 5 10 0 5
[128] 5 5 10 0 10
[129] 5 5 10 5 0
[130] 5 5 10 5 5
[131] 5 5 10 5 10
[132] 5 5 10 10 0
[133] 5 5 10 10 5
[134] 5 5 10 10 10
[135] 5 10 0 0 0
[136] 5 10 0 0 5
[137] 5 10 0 0 10
[138] 5 10 0 5 0
[139] 5 10 0 5 5
[140] 5 10 0 5 10
[141] 5 10 0 10 0
[142] 5 10 0 10 5
[143] 5 10 0 10 10
[144] 5 10 5 0 0
[145] 5 10 5 0 5
[146] 5 10 5 0 10
[147] 5 10 5 5 0
[148] 5 10 5 5 5
[149] 5 10 5 5 10
[150] 5 10 5 10 0
[151] 5 10 5 10 5
[152] 5 10 5 10 10
[153] 5 10 10 0 0
[154] 5 10 10 0 5
[155] 5 10 10 0 10
[156] 5 10 10 5 0
[157] 5 10 10 5 5
[158] 5 10 10 5 10
[159] 5 10 10 10 0
[160] 5 10 10 10 5
[161] 5 10 10 10 10
[162] 10 0 0 0 0
[163] 10 0 0 0 5
[164] 10 0 0 0 10
[165] 10 0 0 5 0
[166] 10 0 0 5 5
[167] 10 0 0 5 10
[168] 10 0 0 10 0
[169] 10 0 0 10 5
[170] 10 0 0 10 10
[171] 10 0 5 0 0
[172] 10 0 5 0 5
[173] 10 0 5 0 10
[174] 10 0 5 5 0
[175] 10 0 5 5 5
[176] 10 0 5 5 10
[177] 10 0 5 10 0
[178] 10 0 5 10 5
[179] 10 0 5 10 10
[180] 10 0 10 0 0
[181] 10 0 10 0 5
[182] 10 0 10 0 10
[183] 10 0 10 5 0
[184] 10 0 10 5 5
[185] 10 0 10 5 10
[186] 10 0 10 10 0
[187] 10 0 10 10 5
[188] 10 0 10 10 10
[189] 10 5 0 0 0
[190] 10 5 0 0 5
[191] 10 5 0 0 10
[192] 10 5 0 5 0
[193] 10 5 0 5 5
[194] 10 5 0 5 10
[195] 10 5 0 10 0
[196] 10 5 0 10 5
[197] 10 5 0 10 10
[198] 10 5 5 0 0
[199] 10 5 5 0 5
[200] 10 5 5 0 10
[201] 10 5 5 5 0
[202] 10 5 5 5 5
[203] 10 5 5 5 10
[204] 10 5 5 10 0
[205] 10 5 5 10 5
[206] 10 5 5 10 10
[207] 10 5 10 0 0
[208] 10 5 10 0 5
[209] 10 5 10 0 10
[210] 10 5 10 5 0
[211] 10 5 10 5 5
[212] 10 5 10 5 10
[213] 10 5 10 10 0
[214] 10 5 10 10 5
[215] 10 5 10 10 10
[216] 10 10 0 0 0
[217] 10 10 0 0 5
[218] 10 10 0 0 10
[219] 10 10 0 5 0
[220] 10 10 0 5 5
[221] 10 10 0 5 10
[222] 10 10 0 10 0
[223] 10 10 0 10 5
[224] 10 10 0 10 10
[225] 10 10 5 0 0
[226] 10 10 5 0 5
[227] 10 10 5 0 10
[228] 10 10 5 5 0
[229] 10 10 5 5 5
[230] 10 10 5 5 10
[231] 10 10 5 10 0
[232] 10 10 5 10 5
[233] 10 10 5 10 10
[234] 10 10 10 0 0
[235] 10 10 10 0 5
[236] 10 10 10 0 10
[237] 10 10 10 5 0
[238] 10 10 10 5 5
[239] 10 10 10 5 10
[240] 10 10 10 10 0
[241] 10 10 10 10 5
[242] 10 10 10 10 10