[英]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 假设我将4作为输入,我想显示类似于:0000到1111的内容
But, if I input 5, I want to display like: 00000 to 11111 and so on. 但是,如果我输入5,我想显示如下:00000到11111,依此类推。
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? EDIT2 :WTH正在发生Stackoverflow,我问过你们有没有为我编写代码? 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. 编辑3 :我在这里发布我尝试过的代码。 I hope this "SATISFIES" all the people who were thinking I have not tried anything.
我希望这个“满意”所有想到我没有尝试过的人。
import java.util.ArrayList;
public class RegularInvestigator { 公共类RegularInvestigator {
public ArrayList createCombinations(ArrayList listOfFlightNumbers) { 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) { 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 :) 编辑4 :谢谢phoxis,PengOne,Jerry Coffin和oliholz的宝贵答案:)
n
n
i=0
to (2^n) - 1
i=0
到(2^n) - 1
计数 i
bitmask each bit of i
and display. i
位掩码的每个值, i
和显示的每一位。 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 要么使用phoxis非常好的解决方案,要么只是按字典顺序迭代它们(这实际上是相同的解决方案!):给定给定长度的二进制字符串,通过找到最右边的零条目获得下一个字典字符串,将其更改为
1
,并且将其右侧的所有内容更改回0
,例如
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).
这只是从0到1计数小于指定的最大值(其中最大值为1左移指定的位数)。
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);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.