我正在嘗試指定每層的節點輸入,例如[1,2,5,3,1]并在
這是javascript代碼:
function getConnectedIndices(m, n) {
let a = new Array(m).fill(0).map((x, i) => (i)/(m) (1/2/m));
let b = new Array(n).fill(0).map((x, i) => (i)/(n) (1/2/n));
let ab = a.map(x => b.map(y => Math.abs(x - y)));
let threshold = 1/Math.max(m, n);
return ab.map(x => x.map((y, i) => y <= threshold ? i : null).filter(x => x !== null));
}
let result = getConnectedIndices(3, 5);
result.forEach((x, i) => console.log(`Node ${i} in first row is connected to nodes (${x}) of the second row`));
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/520472.html
標籤:算法戈多
