var artObjects = ["dbld", "db", "fdm", "hg", "hm", "le", "mef", "mg", "hlbt", "lp", "xtlx", "lw"]
var impossibleObjects = [
["fdm", "lw", "mef"],//第 1 个格子不会出现的物品
["fdm", "xtlx", "hm"],//第 2 个格子不会出现的物品
["dbld", "fdm", "hlbt"],//第 3 个格子不会出现的物品
["fdm", "db", "hg"],//...
["hlbt", "lp", "db"],//...
["lw", "lp", "mef"],
["xtlx", "db", "fdm"],
["mef", "hg", "hlbt"],
["hg", "le", "xtlx"],
["le", "lw", "lp"],
["mg", "hm", "mef"],
["lw", "mg", "le"]
]
报错
<--- Last few GCs --->
[2338:0x108008000] 30104 ms: Scavenge 4052.4 (4129.0) -> 4047.7 (4130.5) MB, 4.0 / 0.0 ms (average mu = 0.210, current mu = 0.145) allocation failure
[2338:0x108008000] 30111 ms: Scavenge 4054.0 (4130.5) -> 4049.1 (4131.8) MB, 3.7 / 0.0 ms (average mu = 0.210, current mu = 0.145) allocation failure
[2338:0x108008000] 30653 ms: Scavenge 4055.4 (4131.8) -> 4050.6 (4147.8) MB, 538.3 / 0.0 ms (average mu = 0.210, current mu = 0.145) allocation failure
<--- JS stacktrace --->
FATAL ERROR: MarkCompactCollector: young object promotion failed Allocation failed - JavaScript heap out of memory
var artObjects = ["女孩", "男孩", "乳牛", "博客", "甜点", "朋克"]
var impossibleObjects = [
["甜点", "朋克", "女孩"],
["乳牛", "甜点", "博客"],
["女孩", "乳牛", "甜点"],
["甜点", "乳牛", "男孩"],
["博客", "女孩", "男孩"],
["朋克", "男孩", "博客"]
]
结果
女孩:[0,41,0,41,0,17]
男孩:[35,29,35,0,0,0]
乳牛:[29,0,0,0,35,35]
博客:[35,0,35,29,0,0]
甜点:[0,0,0,0,52,47]
朋克:[0,29,29,29,11,0]
在 node 环境下如何让脚本在样本等于 MN=12 时正常输出?
数组全排列用的这里 https://github.com/GDUFXRT/NOTES/tree/master/permutation
function permutation(a, m) {
let result = [];
let n = a.length;
m = m || n;
function recur(_a, tmpResult = []) {
if (tmpResult.length === m) {
result.push(tmpResult);
} else {
for (let i = 0; i < _a.length; i++) {
let tmpA = _a.concat();
let _tmpResult = tmpResult.concat();
_tmpResult.push(tmpA[i]);
tmpA.splice(i, 1);
recur(tmpA, _tmpResult);
}
}
}
recur(a);
return result;
}
//12 个物品放在 12 个格子
var artObjects = ["dbld", "db", "fdm", "hg", "hm", "le", "mef", "mg", "hlbt", "lp", "xtlx", "lw"]
var impossibleObjects = [
["fdm", "lw", "mef"],//第 1 个格子不会出现的物品
["fdm", "xtlx", "hm"],//第 2 个格子不会出现的物品
["dbld", "fdm", "hlbt"],//第 3 个格子不会出现的物品
["fdm", "db", "hg"],//...
["hlbt", "lp", "db"],//...
["lw", "lp", "mef"],
["xtlx", "db", "fdm"],
["mef", "hg", "hlbt"],
["hg", "le", "xtlx"],
["le", "lw", "lp"],
["mg", "hm", "mef"],
["lw", "mg", "le"]
]
var allArray = permutation(artObjects)
var arrayGroup = [allArray]
for (var i = 0; i < artObjects.length; i++) {
arrayGroup[i + 1] = []
for (var j = 0; j < arrayGroup[i].length; j++) {
if ((arrayGroup[i][j][i] !== impossibleObjects[i][0]) &&
(arrayGroup[i][j][i] !== impossibleObjects[i][1]) &&
(arrayGroup[i][j][i] !== impossibleObjects[i][2]) &&
(arrayGroup[i][j][i] !== (impossibleObjects[i][3] !== undefined ? impossibleObjects[i][3] : ""))) {
arrayGroup[i + 1].push(arrayGroup[i][j])
}
}
console.log(arrayGroup[i + 1].length)
}
var finalArray = arrayGroup[arrayGroup.length - 1]
var resultGroup = {}
for (var i = 0; i < artObjects.length; i++) {
resultGroup[artObjects[i]] = []
for (var a = 0; a < artObjects.length; a++) {
resultGroup[artObjects[i]][a] = []
}
for (var f = 0; f < finalArray.length; f++) {
for (var t = 0; t < artObjects.length; t++) {
if (finalArray[f][t] == artObjects[i]) {
resultGroup[artObjects[i]][t].push(finalArray[f])
}
}
}
console.log(artObjects[i] + ":" + JSON.stringify(resultGroup[artObjects[i]].map((count) => Math.floor(count.length / finalArray.length * 100))))
}
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.