Java: Generator of true's & false's combinations by giving the number N;

后端 未结 7 2296
野的像风
野的像风 2020-12-09 13:53

I tied to simplify the task as much as possible, so I could apply it to my algorithm.

And here is the challenge for mathematicians and programmers:

I need to

7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-09 14:07

    This should do the trick

    int cols = 3;
    int rows = (int) Math.pow(2, cols);
    for (int row = 0; row < rows; row++)
        System.out.println(String.format("%" + cols + "s", 
                Integer.toBinaryString(row)).replace(' ', '0').replace('1', 'X'));
    

    out:

    000
    00X
    0X0
    0XX
    X00
    X0X
    XX0
    XXX
    

提交回复
热议问题