[英]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.