[英]C : 0 & 1 combinations using recursion
我想根据变量编号(数字)使用c递归列出o&1的组合
我想要的输出是
000
001
010
011
100
101
110
111
我尝试了很多算法,最后一个是:
void permute(unsigned number) {
if(number == 0) {
printf("\n");
return;
}
permute(number - 1);
printf("0");
permute(number - 1);
printf("1");
} //permute ends here
void permuteN(unsigned number) {
unsigned i;
for(i = 0; i < number + 1; i++){
permute(i);
}
} //permuteN ends here
我认为它给了我答案,但是没有命令,因为我不知道在哪里放置\\ n;
需要你的帮助!
如果您确实只是在寻找1
和0
的组合,我建议您只计算数字并以二进制形式列出。
以二进制数字0...7
并仅取最后3位(可能应用掩码),最后得到与指定的相同的集合:
000
001
...
...
111
对于n位数字组合,您需要执行0..2^n - 1
基于此答案 ,对于一种特定的3位情况(贷记为@ChrisLutz和@dirkgently)
#include <stdio.h>
int main(){
int numdigits = 3, j;
for(j=1; j<8; j++)
printbits(j);
}
void printbits(unsigned char v) {
int i;
for(i = 2; i >= 0; i--) putchar('0' + ((v >> i) & 1));
printf("\n");
}
输出:
000
001
010
011
100
101
110
111
您实际上所做的就是将数字转换为二进制。...一个简单的循环就可以完成此操作,而无需任何库调用(除了printf
)...
const unsigned int numbits = 3;
unsigned int bit;
for( bit = 1U << (numbits-1); bit != 0; bit >>= 1 ) {
printf( number&bit ? "1" : "0" );
}
printf( "\n" );
编辑,因为您似乎想要递归。 您需要某种方式来指定所需的位数。 您需要将此传递到递归例程中:
#include <stdio.h>
void permute(unsigned number, unsigned bits)
{
if( bits == 0 ) return;
permute(number / 2, bits-1);
printf( "%d", number % 2 );
} //permute ends here
void permuteN(unsigned number, unsigned bits ) {
unsigned i;
for(i = 0; i < number + 1; i++){
permute(i, bits);
printf("\n");
}
} //permuteN ends here
int main(void)
{
permuteN(7, 3);
return 0;
}
要按所需顺序获得输出,您不知道何时编写换行符。 因此,在这种情况下,请稍后再编写。
@paddy有一个不错的答案; 只是增加了一点(从您对我的评论的回复中我得出的强项-游戏有点迟了)。 这依赖于pow(),(和log10以获得打印效果),如此; 如果使用gcc与-lm
编译:
base
可能在这里有点令人困惑-但是您猜对了意思。
gcc -Wall -Wextra -pedantic -o combo combo.c -lm
/* gcc - Wall -Wextra -pedantic -o combo combo.c -lm */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
static void prnt_combo(unsigned number, unsigned bits, int base)
{
if (!bits)
return;
prnt_combo(number / base, --bits, base);
printf("%d", number % base);
}
void prnt_combos(int bits, int base)
{
int i;
int n = pow(base, bits);
int wp = log10(n) + 1;
fprintf(stderr,
"Printing all combinations of 0 to %d by width of %d numbers. "
"Total %d.\n",
base - 1, bits, n
);
for (i = 0; i < n; i++) {
fprintf(stderr, "%*d : ", wp, i);
prnt_combo(i, bits, base);
printf("\n");
}
}
/* Usage: ./combo [<bits> [<base>]]
* Defaults to ./combo 3 2
* */
int main(int argc, char *argv[])
{
int bits = argc > 1 ? strtol(argv[1], NULL, 10) : 3;
int base = argc > 2 ? strtol(argv[2], NULL, 10) : 2;
prnt_combos(bits, base);
return 0;
}
样品:
$ ./combo 4 2
Printing all combinations of 0 to 1 by width of 4 numbers. Total 16.
0 : 0000
1 : 0001
2 : 0010
3 : 0011
4 : 0100
5 : 0101
6 : 0110
7 : 0111
8 : 1000
9 : 1001
10 : 1010
11 : 1011
12 : 1100
13 : 1101
14 : 1110
15 : 1111
或清除输出:
$ ./combo 3 2 >&2-
000
001
010
011
100
101
110
111
您可能想要添加以下内容:
if (base > 10)
printf("%x", number % base);
else
printf("%d", number % base);
在prnt_combo()
。 这样你得到即2:16
0 : 00
1 : 01
2 : 02
3 : 03
4 : 04
...
250 : fa
251 : fb
252 : fc
253 : fd
254 : fe
255 : ff
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.