Java 生成二进制序列
我想生成每一个可能的二进制数字序列,其中列表中的每一个序列都被限制为一个特定的数字1,并且有零的填充,以使每个列表具有相同的长度 例如,如果序列的长度应为4个数字,且有2个数字,则所有序列应为:Java 生成二进制序列,java,binary,Java,Binary,我想生成每一个可能的二进制数字序列,其中列表中的每一个序列都被限制为一个特定的数字1,并且有零的填充,以使每个列表具有相同的长度 例如,如果序列的长度应为4个数字,且有2个数字,则所有序列应为: 1100 1010 1001 0110 0101 0011 数字前面的零会保留下来。试试这个: //Edit your min and max, e.g. Integer.MAX_VALUE and Integer.MIN_VALUE int min = 0; int max = 10; for(in
1100 1010 1001 0110 0101 0011
数字前面的零会保留下来。试试这个:
//Edit your min and max, e.g. Integer.MAX_VALUE and Integer.MIN_VALUE
int min = 0;
int max = 10;
for(int i = min; i < max; i++){
//Make 16 bit long binary Strings
String s = String.format("%16s", Integer.toBinaryString(i)).replace(' ', '0');
//Make 4 bits long chunks
List<String> chunks = new ArrayList<>();
Matcher matcher = Pattern.compile(".{0,4}").matcher(s);
while (matcher.find()) {
chunks.add(s.substring(matcher.start(), matcher.end()));
}
StringBuilder b = new StringBuilder();
for (String c : chunks) {
//Here you can count the 1 and 0 of the current chunk with c.charAt(index)
b.append(c);
b.append(" ");
}
System.out.println(b.toString());
}
//编辑最小值和最大值,例如Integer.max\u值和Integer.min\u值
int min=0;
int max=10;
对于(int i=min;i
如果要保留0,只需添加填充:
int end = 100; //Change this
for (int i = 0; i <= end; i++) {
String bytestring = Integer.toBinaryString(i);
String padding = "00000000000000000000000000000000";
bytestring = padding.substring(0, 32 - bytestring.length()) + bytestring;
System.out.println(bytestring);
}
int-end=100//改变这个
对于(int i=0;i,可以使用递归函数调用解决此问题:
public class BinarySequences {
public static void main(String[] args) {
final int numCount = 4;
final int oneCount = 2;
checkSubString(numCount, oneCount, "");
for (String res : results) {
System.out.println(res);
}
}
private static List<String> results = new ArrayList<>();
private static void checkSubString(int numCount, int oneCount, String prefix) {
if ((numCount >= oneCount) && (oneCount >= 0)) {
if (numCount==1) {
if (oneCount==1) {
results.add(prefix + "1");
} else {
results.add(prefix + "0");
}
} else {
checkSubString(numCount-1, oneCount , prefix + "0");
checkSubString(numCount-1, oneCount-1, prefix + "1");
}
}
}
}
公共类二进制序列{
公共静态void main(字符串[]args){
最终整数计数=4;
最终int oneCount=2;
checkSubString(numCount,oneCount,“”);
for(字符串res:results){
系统输出打印项次(res);
}
}
私有静态列表结果=新建ArrayList();
私有静态void checkSubString(int numCount、int oneCount、字符串前缀){
如果((numCount>=oneCount)&&(oneCount>=0)){
如果(numCount==1){
如果(oneCount==1){
结果。添加(前缀+“1”);
}否则{
结果。添加(前缀+“0”);
}
}否则{
checkSubString(numCount-1,oneCount,前缀+“0”);
checkSubString(numCount-1,oneCount-1,前缀+“1”);
}
}
}
}
需要org.apache.commons.lang.StringUtils
,但这使得它很短:
final int digits = 4;
final int onesrequired = 2;
int maxindex = (int) Math.pow(2, digits);
for (int i = 0; i < maxindex; i++) {
String binaryStr = Integer.toBinaryString(i);
if (StringUtils.countMatches(binaryStr, "1") == onesrequired) {
System.out.print(String.format("%" + digits + "s", binaryStr).replace(' ', '0') + " ");
}
}
最终整数位数=4;
要求的最终整数=2;
int maxindex=(int)Math.pow(2位);
对于(int i=0;i
java中内置了一个名为Integer.toBinaryString(n)的函数;但是如何将它限制为只有特定数量的序列?@stigtore您可以创建一个大小合适的字符串,其中包含所需的0
和1
(例如String a=“0011”
),并使用中的代码生成所有排列/字谜。