[英]How can I print the number of combination and not the actual combination? Java
I'd like to print the number of combination and not the actual combination of bits.我想打印组合的数量而不是实际的位组合。 How can I code that?我该如何编码? I'm looking forward for some solution.我期待着一些解决方案。 Thank you!谢谢!
The Task:任务:
Write a program that accepts a number.编写一个接受数字的程序。 This number corresponds to the number of bits to be taken into account.该数字对应于要考虑的位数。 The program should then display on the screen how many binary combinations there are that do not consist of two adjacent 1s.然后程序应该在屏幕上显示有多少二进制组合不由两个相邻的 1 组成。 For example, given a 3-bit number, there are 5 out of 8 possible combinations.例如,给定一个 3 位数,8 种可能的组合中有 5 种。
import java.util.Scanner;
public class BinaryS {
public static String toString(char[] a) {
String string = new String(a);
return string;
}
static void generate(int k, char[] ch, int n) {
if (n == k) {
for (int i = 0; i < ch.length; i++) {}
System.out.print(toString(ch) + " ");
return;
}
// If the first Character is
//Zero then adding**
if (ch[n - 1] == '0') {
ch[n] = '0';
generate(k, ch, n + 1);
ch[n] = '1';
generate(k, ch, n + 1);
}
// If the Character is One
// then add Zero to next**
if (ch[n - 1] == '1') {
ch[n] = '0';
// Calling Recursively for the
// next value of Array
generate(k, ch, n + 1);
}
}
static void fun(int k) {
if (k <= 0) {
return;
}
char[] ch = new char[k];
// Initializing first character to Zero
ch[0] = '0';
// Generating Strings starting with Zero--
generate(k, ch, 1);
// Initialized first Character to one--
ch[0] = '1';
generate(k, ch, 1);
}
public static void main(String args[]) {
System.out.print("Number: ");
Scanner scanner = new Scanner(System.in);
int k = scanner.nextInt();
//Calling function fun with argument k
fun(k);
}
}
The program actually works fine, my only problem is I would like to print the number of combinations and not the actual combination.该程序实际上运行良好,我唯一的问题是我想打印组合的数量而不是实际的组合。 For example for the input 3 we get 000 001 010 100 101 which is 5.例如,对于输入 3,我们得到 000 001 010 100 101,即 5。
Unfortunately, your code has some problems.不幸的是,您的代码有一些问题。 For one you have an empty forloop in the generate method.对于其中一个,您在 generate 方法中有一个空的 forloop。 However, I can help you get the count by doing it a different way and printing the results.但是,我可以通过不同的方式来帮助您计算并打印结果。 Forgetting about the loop that goes from 2 to 20
, here is what is going on.忘记从2 to 20
的循环,这是正在发生的事情。 And this may not be most efficient way of finding the matches but for short runs it exposes the counts as a recognizable pattern (which could also be determined by mathematical analysis).这可能不是找到匹配项的最有效方法,但对于短期运行,它会将计数显示为可识别的模式(也可以通过数学分析来确定)。
IntPredicate
that checks for adjacent one bits by masking the lower order two bits.首先,创建一个IntPredicate
,它通过屏蔽较低的两位来检查相邻的一位。n
is the number of bits.生成一个从0到2 n的 IntStream,其中n
是位数。1
bits.然后将上述谓词与过滤器一起使用,对每个不包含两个相邻1
位的值进行计数。IntPredicate NoAdjacentOneBits = (n)-> {
while (n > 0) {
if ((n & 3) == 3) {
return false;
}
n>>=1;
}
return true;
};
for (int n = 1; n <= 20; n++) {
long count = IntStream.range(0, (int) Math.pow(2, n))
.filter(NoAdjacentOneBits).count();
System.out.println("For n = " + n + " -> " + count);
}
prints (with annotated comments on first three lines)印刷品(前三行带有注释)
For n = 1 -> 2 // not printed but would be 0 and 1
For n = 2 -> 3 // 00, 01, 10
For n = 3 -> 5 // 000, 001, 010, 100, 101
For n = 4 -> 8
For n = 5 -> 13
For n = 6 -> 21
For n = 7 -> 34
For n = 8 -> 55
For n = 9 -> 89
For n = 10 -> 144
For n = 11 -> 233
For n = 12 -> 377
For n = 13 -> 610
For n = 14 -> 987
For n = 15 -> 1597
For n = 16 -> 2584
For n = 17 -> 4181
For n = 18 -> 6765
For n = 19 -> 10946
For n = 20 -> 17711
The counts are directly related to the n th term of the Fibonacci Series that starts with 2 3 5 8. .计数与斐波那契数列的第n项直接相关,该数列以 2 3 5 8 开头。 . .
So you really don't even need to inspect the values for adjacent bits.所以你真的甚至不需要检查相邻位的值。 Just compute the related term of the series.只需计算该系列的相关项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.