二元排列的打印清单 [英] Print list of binary permutations

查看:201
本文介绍了二元排列的打印清单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所试图做的是打印的二进制数n位数做好一切准备。换句话说,用4位数字:

What I am trying to do is print all the possibilities of a binary number n digits long. In other words, with a 4 digit number:

0001
0010
0100
1000

...等

说实话,我不知道在哪里即使有这样的开始(而不是我想我可能需要使用一个循环,并可能是一个数组),所以任何指针朝着正确的方向将是AP preciated。

To be honest, I have no idea of where to even start with this (other than I figure I'd need to use a loop, and probably an array) so any pointers in the right direction would be appreciated.

推荐答案

也许你可以使用一个递归算法:

Maybe you could use a recursive algorithm:

public void printBin(String soFar, int iterations) {
    if(iterations == 0) {
        System.out.println(soFar);
    }
    else {
        printBin(soFar + "0", iterations - 1);
        printBin(soFar + "1", iterations - 1);
    }
}

您可以执行这个是这样的:

You would execute this like this:

printBin("", 4);

这会给你一切可能的二进制数与4位。

That would give you all possible binary numbers with 4 digits.

希望这有助于!

这篇关于二元排列的打印清单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆