2016-09-27 29 views
2

我想創建一個包含三個變量的所有可能組合的數組,它們可以是真或假(即8種可能的組合)。生成所有可能的真/假組合

我想在這個圖像

enter image description here

建立在左上角的立方體所以輸出應該是這樣的

points = [ 
    // first square 
    { 
    id: '000', 
    truths: [false, false, false] 
    position: [0, 0] 
    }, 
    { 
    id: '100', 
    truths: [true, false, false] 
    position: [5, 0] 
    }, 
    { 
    id: '010', 
    truths: [false, true, false] 
    position: [0, 5] 
    }, 
    { 
    id: '110', 
    truths: [true, true, false] 
    position: [5, 5] 
    }, 
    // second square 
    { 
    id: '001', 
    truths: [false, false, true] 
    position: [2.5, 2.5] 
    }, 
    { 
    id: '101', 
    truths: [true, false, true] 
    position: [7.5, 2.5] 
    }, 
    { 
    id: '011', 
    truths: [false, true, true] 
    position: [2.5, 7.5] 
    }, 
    { 
    id: '111', 
    truths: [true, true, true] 
    position: [7.5, 7.5] 
    }, 
]; 

lines = [ 
    { from: '000', to: '100' }, 
    { from: '000', to: '010' }, 
    { from: '000', to: '001' }, 

    { from: '100', to: '101' }, 
    { from: '100', to: '110' }, 

    { from: '001', to: '101' }, 
    { from: '001', to: '011' }, 

    { from: '101', to: '001' }, 
    { from: '101', to: '111' }, 

    ... 
] 

我不知道怎麼走通過所有可能的真值來創造這些點。

一種方法可以是使用一個for循環

for (var i=0; i<Math.pow(2, 3); i++) { 
    ... 
} 

,但它並不能幫助我分配可能的真值。

+1

有2^n個可能的值。如果你不想使用嵌套for循環(你真的不應該),然後提取整數'0 ... 2^n'的位。 「真理」中的「n」值將是整數的位。 – plasmacel

+0

我只是不明白,如果您的訂單是0,4,2,3,1,5,7,8如何二進制的方法將幫助你。你爲什麼不使用數字。 – Redu

+0

@Redu我不明白你在說什麼。訂單無關緊要。從0到8的所有整數將表示3位,這對應於OP的類比中的「真值」數組。 2^n個整數= 2^n個「真值」數組。在二進制中,數字可以被認爲是「數組」:0 = [0,0,0],1 = [0,0,1],2 = [0,1,0],3 = [0,1 ,1],4 = [1,0,0],5 = [1,0,1],6 = [1,1,0],7 = [1,1,1]。 – plasmacel

回答

3

計算機中的所有東西都是二進制的。你不需要任何花式的Math.pow或類似的。

for (let i = 0; i < 1 << 3; i++) { 
 
    console.log([!!(i & (1<<2)), !!(i & (1<<1)), !!(i & 1)]); 
 
}

雖然這看起來不錯,總之,我其實不是!!或幻數風扇。儘管編寫代碼片段時我總是傾向於使用這些技巧。因此將試圖給出一個稍微乾淨的版本:

const AMOUNT_OF_VARIABLES = 3; 
 

 
for (let i = 0; i < (1 << AMOUNT_OF_VARIABLES); i++) { 
 
    let boolArr = []; 
 
    
 
    //Increasing or decreasing depending on which direction 
 
    //you want your array to represent the binary number 
 
    for (let j = AMOUNT_OF_VARIABLES - 1; j >= 0; j--) { 
 
    boolArr.push(Boolean(i & (1 << j))); 
 
    } 
 
    
 
    console.log(boolArr); 
 
}

+0

嗨,只是因爲好奇心問......這個'0'('讓我= 0;')等於一個二進制'0'?和1 >>> 1'一樣嗎? –

+0

'0'確實爲零。只要看看它,javascript二進制運算符就像在32位整數上工作一樣,我們只使用三個位:000,001,010,011,100,101,111。只是省略了前29個零,不在乎那些。 – ASDFGerte

+0

好的,謝謝你的澄清!我習慣了類C的東西(我通常使用'\ 0'),所以有時我會感到困惑。 –

1

這很容易,只是轉換爲0的所有整數通過2**n-1二進制:

var n = 3, 
 
    m = 1 << n; 
 
for (var i = 0; i < m; i++) { 
 
    var s = i.toString(2); // convert to binary 
 
    s = new Array(n + 1 - s.length).join('0') + s; // pad with zeroes 
 
    console.log(s); 
 
}

上面的代碼是一般的;你可以將n更改爲你想要的位數。

1

pow(2, n)可能的值。

在二進制,數字可被簡單地認爲是比特 「陣列」:0 = [0,0,0],1 = [0,0,1],2 = [0,1,0],3 = [0,1,1],4 = [1,0,0],5 = [1,0,1],6 = [1,1,0],7 = [1,1,1]

遵循這個想法,最簡單的方法是提取整數[0, pow(2, n) - 1]的位。以下代碼是上述思想的直接實現:

function test() 
{ 
    var n = 3; 
    var k = (1 << n); // bit trick for pow(2, n) 

    var truths = []; 

    for (var i = 0; i < k; ++i) 
    { 
     truths[i] = []; 

     for (var j = 0; j < n; ++j) 
     { 
     var value = (i >> j) & 1; // extract the j-th bit of i 
     truths[i][j] = value; 
     } 

     console.log(truths[i]); 
    } 
} 
相關問題