简体   繁体   中英

List of all binary combinations for a number in Java

I am working on a project involving "Dynamic Programming" and am struck on this trivial thing, please help.

Suppose I take 4 as an input, I want to display something like: 0000 to 1111

But, if I input 5, I want to display like: 00000 to 11111 and so on.

Thanks in advance,

EDIT : Please don't post asking me for the code. This is not a homework problem and I don't need any code, just tell me the logic for it and I would be happy.

EDIT2 : WTH is happening with Stackoverflow, did I ask any of you to write code for me? I want the person who downvoted to upvote it. What is a point of this forum if I can't for help?

Share the logic with me. We can discuss and I do not require the code for this.

EDIT3 : Here I am posting the code which I tried. I hope this "SATISFIES" all the people who were thinking I have not tried anything.

import java.util.ArrayList;

public class RegularInvestigator {

public ArrayList createCombinations(ArrayList listOfFlightNumbers) {

ArrayList<String> result = new ArrayList<String>();

for(int i = 1; i < listOfFlightNumbers.size(); i++) {

  String binaryEqvivalent = Integer.toBinaryString(i);System.out.println(binaryEqvivalent);
  String element = "";

  for(int j = 0; j < binaryEqvivalent.length(); j++)
    if(binaryEqvivalent.charAt(j) == '1')
      element += listOfFlightNumbers + " ";

  result.add(element.substring(0, element.length() - 1));
}

return result;

}

private String getContent(ArrayList<String> flight) {
String temp = "";

for(int i = 0; i < flight.size() - 1; i++)  temp += flight.get(i) + " ";

temp += flight.get(flight.size() - 1);

return temp;

}

private ArrayList removeElementAtIndex(ArrayList flight, int position) {

ArrayList<String> res = new ArrayList<String>();

for(int i = 0; i < flight.size(); i++) {
  if(i != position) res.add(flight.get(i));
}

return res;

} }

EDIT4 : Thank you phoxis, PengOne, Jerry Coffin and oliholz for your valuable answers :)

  • Get input n
  • Count from i=0 to (2^n) - 1
  • for each value of i bitmask each bit of i and display.
public void outBinary(int value){
   for (int i = 0; i < Math.pow(2, value); i++) {
       System.out.println(Integer.toBinaryString(i));
   }
}

with leading zeros something like that

    for (int i = 0; i < Math.pow(2, value); i++) {
        StringBuilder binary = new StringBuilder(Integer.toBinaryString(i));
        for(int j = binary.length(); j < value; j++) {
            binary.insert( 0, '0' );
        }
        System.out.println(binary);
    }

Either use phoxis's very nice solution, or just iterate them lexicographically (this is really the same solution!): Given a binary string of a given length, get the next lexicographic string by finding the rightmost zero entry, change it to a 1 , and change everything to the right of it back to a 0 , eg

0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1

I'm a bit lost as to how you'd apply dynamic programming to this. It's just a matter of counting from 0 to one less than the specified maximum value (where the maximum value is 1 shifted left the specified number of bits).

Edit: I should add that there are other possibilities (eg, gray codes) but absent some reason to do otherwise, simple binary counting is probably the simplest to implement.

int x = 5;

for(int i = 0; i < (1 << x); i++){ 
 System.out.println(Integer.toBinaryString(i)); 
}

here is the code is to find the combination

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package rotateimage;

/**
 *
 * @author ANGEL
 */
public class BinaryPermutaion {

    public static void main(String[] args) {
        //object creation
        BinaryPermutaion binaryDigit=new BinaryPermutaion();
        //Recursive call of the function to print the binary string combinations
        binaryDigit.printBinary("", 4);
    }

    /**
     * 
     * @param soFar String to be printed
     * @param iterations number of combinations
     */
    public void printBinary(String soFar, int iterations) {
    if(iterations == 0) {
        System.out.println(soFar);
    }
    else {
        printBinary(soFar + "0", iterations - 1);
        printBinary(soFar + "1", iterations - 1);
    }
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM