日期:2014-05-20  浏览次数:20795 次

二进制表示数组生成
想得到这样的数字比如输入3,得到类似下面的数组
000
001
010
011
100
101
110
111

------解决方案--------------------
2的三次方
然后用
Integer.toBinaryString(int i)
就行了
------解决方案--------------------
Java code

    public static void main(String[] args) {
        int a = 0;
        String align = "";
        for (int i = 0; i < 6; i++) {
            a += 1 << i;
            align += "0";
        }
        
        String[] r = new String[a + 1];
        for (int i = 0; i <= a; ++i) {
            String t = Integer.toBinaryString(i);
            String s = (align + t);
            s = s.substring(t.length());
            System.out.println(s);
        }
    }

------解决方案--------------------
Java code
import java.text.DecimalFormat; 
import java.util.Scanner;

public class Test_2 {
static public void print(double i){
String[] init=new String[(int)Math.pow(2.0, i)];
DecimalFormat df=new DecimalFormat("000");
for(int a=0;a <init.length;a++){
init[a]=df.format(Double.parseDouble(Integer.toBinaryString(a)));
System.out.println(init[a]);
}
}
public static void main(String[] args) {
Scanner sca=new Scanner(System.in);
double i=sca.nextDouble();
print(i);

}

}

输入3的时候的测试结果:
000
001
010
011
100
101
110
111

------解决方案--------------------
Java code
public class Test {

    public static void main(String[] args) {
        String[] strs = getBinaries(3);
        for(int i = 0; i < strs.length; i++) {
            System.out.println(strs[i]);
        }
    }
    
    public static String[] getBinaries(int len) {
        if(len < 0) {
            throw new IllegalArgumentException("len must be positive number");
        }
        if(len > 30) {
            throw new IllegalArgumentException("len is too many");
        }
        char[] chs = new char[len];
        int count = 1 << len;
        String[] strs = new String[count];
        for(int i = 0; i < count; i++) {
            strs[i] = getBinary(i, chs);
        }
        return strs;
    }
    
    private static String getBinary(long num, char[] chs) {
        for(int i = 0, k = chs.length - 1; i <= k; i++) {            
            chs[k - i] = (char)((num & 1) + '0');
            num >>= 1;
        }
        return new String(chs);
    }
}

------解决方案--------------------
Java code
public class Test {
    public static void main(String[] args) {
        int value = 3;
        int maxValue = (int)Math.pow(2, 3);
        String[] result = new String[maxValue];
        while(maxValue > 0) {
            result[maxValue -- - 1] = String.format("%3s", Integer.toBinaryString(maxValue)).replaceAll(" ", "0");
        }
        for(int i = 0; i < result.length; i ++) {
            System.out.println(result[i]);
        }
    }
}

------解决方案--------------------
来个简单一点的:
Java code
import java.text.DecimalFormat;

public class Gen
{
    public static String genStr(int len)
    {
        for (int i = 0; i < (1 << len); i++)
        {
            System.out.println(new DecimalFormat("000").format(Double.parseDouble(Integer.toBinaryString(i))));
        }
        return null;
    }

    public static void main(String[] args)
    {
        Gen.genStr(3);
    }

}