Printing a list of binary permutations

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

0001 0010 0100 1000 

.. etc.

Honestly, I have no idea where to even start from this (besides, I suppose I will need to use a loop and probably an array), so any pointers in the right direction will be appreciated.

+6
source share
6 answers

Perhaps 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 do it as follows:

 printBin("", 4); 

This will give you all possible 4-digit binary numbers.

Hope this helps!

+13
source

For a binary number n -bit, there are 2 ^ n permutations. You just need to iterate over integers from 0 to (1<<n)-1 and convert each of them to binary.

+5
source
 for(int i=0; i < 128; i++){ System.out.println(Integer.toBinaryString(i)); } 

Adjust the maximum size as high as you want.

If you need padded 0s, we had another question: Insert a binary string equal to zero ("0") with leading zeros in Java

+4
source

This helps to find out how many possibilities there are.

2^4 = 16 , right?

This will help to find out this one .

Here's how I would do it:

 /** * BinaryDemo * @author Michael * @since 12/10/11 */ public class BinaryDemo { public static void main(String[] args) { if (args.length > 0) { int n = Integer.parseInt(args[0]); int m = 1; for (int i = 1; i <= n; ++i) { m *= 2; } System.out.println("# bits : " + n); System.out.println("# values: " + m); String format = "%" + n + "s"; for (int i = 0; i < m; ++i) { System.out.println(String.format(format, Integer.toString(i, 2))); } } else { System.out.println("Usage: BinaryDemo <n>"); } } } 
+1
source

To find all possible permutations of a given binary string (template), for example

Permutations of 1000 are 1000, 0100, 0010, 0001

 void permutation(int no_ones, int no_zeroes, string accum){ if(no_ones == 0){ for(int i=0;i<no_zeroes;i++){ accum += "0"; } cout << accum << endl; return; } else if(no_zeroes == 0){ for(int j=0;j<no_ones;j++){ accum += "1"; } cout << accum << endl; return; } permutation (no_ones - 1, no_zeroes, accum + "1"); permutation (no_ones , no_zeroes - 1, accum + "0"); } int main(){ string append = ""; //finding permutation of 11000 permutation(2, 6, append); //the permutations are //11000 //10100 //10010 //10001 //01100 //01010 cin.get(); } 
0
source

My solutions use a backtracking algorithm. Just print all the combinations using "0" and then "1". This is no better than the accepted answer, but another way to solve the problem. As an example, just call the function as shown below from main ()

 ArrayList<Integer> chosen = new ArrayList<>(); printAllBinaryDigits(4, chosen); private static void printAllBinaryDigits(int digits, ArrayList<Integer>chosen) { if (digits == 0) { System.out.println(chosen); } else { chosen.add(0); printAllBinaryDigits(digits-1, chosen); chosen.remove(chosen.size()-1); chosen.add(1); printAllBinaryDigits(digits-1, chosen); chosen.remove(chosen.size()-1); } } 
0
source

Source: https://habr.com/ru/post/903541/


All Articles