我需要遍历一个n
元素数组(因此是动态大小),例如4,就好像它是一个二进制数,从全零({0,0,0,0}
)开始。每次需要递增就像是二进制一样,{0,0,0,0}
- > {0,0,0,1}
- > {0,0,1,0}
- > {0,0,1,1}
- > {0,1,0,0}
等等......
我在生成一个使用数组值的算法时遇到了麻烦,我考虑过使用递归但是除了ifs中的硬编码之外,找不到这样做的方法。我想我可以生成一个n
位数字然后应用this post中讨论的任何算法,但这样做会不够优雅;当我需要使用数组时,OP要求打印n位数字。
如果有人能指出我正确的方向,那就太好了 上下文:
int n;
scans("%d", &n);
int *arr = calloc(n, sizeof(int));
答案 0 :(得分:4)
算法:
对不起,我无法提供代码示例。
答案 1 :(得分:1)
您似乎想要做的是实现binary addition(或者更准确地说是二进制计数器),无论如何使用逻辑门在CPU的数字电路中实现。可以使用逻辑运算( nand 和 xor )的组合来完成它。
但根据我的优雅感,最优雅的方法将使用CPU的先天能力来增加数字并编写一个函数来将数字分解为位数组:< / p>
void generate_bit_array(unsigned int value, uint8_t *bit_array, unsigned int bit_count) {
for (unsigned int i=0; i<bit_count; i++) {
bit_array[i] = value >> (bit_count - i - 1) & 0x01;
}
}
int main(int argc, void **argv) {
unsigned int i;
uint8_t my_array[4];
/* Iterate and regenerate array's content */
for (i=0; i<4; i++) {
generate_bit_array(i, my_array, 4);
/* Do something */
}
return 0;
}
答案 2 :(得分:1)
如果我理解正确,那么您需要的是以下内容
#include <stdio.h>
#define N 4
void next_value( unsigned int a[], size_t n )
{
unsigned int overflow = 1;
for ( size_t i = n; i != 0 && overflow; i-- )
{
overflow = ( a[i-1] ^= overflow ) == 0;
}
}
int empty( const unsigned int a[], size_t n )
{
while ( n && a[n-1] == 0 ) --n;
return n == 0;
}
int main(void)
{
unsigned int a[N] = { 0 };
do
{
for ( size_t i = 0; i < N; i++ ) printf( "%i ", a[i] );
putchar( '\n' );
next_value( a, N );
} while ( !empty( a, N ) );
return 0;
}
程序输出
0 0 0 0
0 0 0 1
0 0 1 0
0 0 1 1
0 1 0 0
0 1 0 1
0 1 1 0
0 1 1 1
1 0 0 0
1 0 0 1
1 0 1 0
1 0 1 1
1 1 0 0
1 1 0 1
1 1 1 0
1 1 1 1
或者你可以编写一个计算下一个值的函数,这样如果下一个值等于0,那么函数返回0.
例如
#include <stdio.h>
#include <stdlib.h>
int next_value( unsigned int a[], size_t n )
{
unsigned int overflow = 1;
for ( ; n != 0 && overflow; n-- )
{
overflow = ( a[n-1] ^= overflow ) == 0;
}
return overflow == 0;
}
int main(void)
{
size_t n;
printf( "Enter the length of the binary number: " );
if ( scanf( "%zu", &n ) != 1 ) n = 0;
unsigned int *a = calloc( n, sizeof( unsigned int ) );
do
{
for ( size_t i = 0; i < n; i++ ) printf( "%i ", a[i] );
putchar( '\n' );
} while ( next_value( a, n ) );
free( a );
return 0;
}
程序输出可能看起来像
Enter the length of the binary number: 3
0 0 0
0 0 1
0 1 0
0 1 1
1 0 0
1 0 1
1 1 0
1 1 1
答案 3 :(得分:1)
算法:
从最右边的元素开始。
如果它为零,则将其设置为1并退出
一定是一个人;将其设为零
如果您位于最左侧的元素,请退出。
你还没有离开最左边的元素;向左移动一个元素并重复。
这是Hymie的算法,我会尝试用它编写代码:
#include <stdlib.h>
#include <stdio.h>
int my_algo(int *arr, int size) {
for (int i = size - 1; i >= 0; i--) { // Start at the rightmost element
if (arr[i] == 0) { // If it's a zero, then set it to one and exit
arr[i] = 1;
return 1;
} else if (arr[i] == 1) { // If it's a one, set it to zero and continue
arr[i] = 0;
}
}
return 0; // stop the algo if you're at the left-most element
}
int main() {
int n;
scanf("%d", &n);
int *arr = calloc(n, sizeof(int));
do {
for (int i = 0; i < n; i++) {
putchar(arr[i] + '0');
}
putchar('\n');
} while (my_algo(arr, n));
return (0);
}
此算法是动态的,可与scanf配合使用。 这是4的结果:
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
答案 4 :(得分:1)
你可以这样做:
#include <stdio.h>
#include <stdlib.h>
void gen_bit_array(int *numarr, int n, int arr_size) {
if(n > 0) {
numarr[arr_size-n] = 0;
gen_bit_array(numarr, n-1, arr_size);
numarr[arr_size-n] = 1;
gen_bit_array(numarr, n-1, arr_size);
} else {
int i;
for(i=0; i<arr_size; i++)
printf("%d", numarr[i]);
printf ("\n");
}
}
int main() {
int n,i;
printf ("Enter array size:\n");
scanf("%d", &n);
int *numarr = calloc(n, sizeof(int));
if (numarr == NULL)
return -1;
gen_bit_array(numarr, n, n);
return 0;
}
输出:
Enter array size:
2
00
01
10
11
Enter array size:
4
0000
0001
0010
0011
0100
0101
0110
0111
1000
1001
1010
1011
1100
1101
1110
1111
答案 5 :(得分:0)
不需要算法,你可以建立一个函数,
如果数组是固定大小,这个过程可能是一个好主意:
所有这些过程都可以使用移位(&gt;&gt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&lt;&quot;和掩码(例如&amp; 255))来完成。