我被一个基本的算法问题困住了,我想不出如何解决它。
基本上,我想列出所有的数字,都是一个整数的正确后代。也就是说,如果我的数字是21,我想使用它的二进制表示(10101),并列出至少有一个公共比特值为21且小于21的所有数字。这里的结果应该是10100,10001,10000,101,100,1。
正确后代的数学定义如下:
h = d0 + d1*2^1 + ... + dm-1*2^(m-1)的非负数,其中di =0或1.h' = d0' + d1'*2^1 + ... + dm-1'*2^(m-1),其中di’=0或1.的h if’<=di的后代。
我尝试过Python和C中的许多实现,并尝试了旧的笔和纸技术,但都失败了。我知道这很简单,但我似乎搞不懂。我正在用C编写代码,所以如果你找到一个在C中工作的解决方案,那将是理想的,但我现在可以接受任何东西。
发布于 2021-01-19 20:53:14
这里有一个非常简单的方法:枚举n-1和1之间的所有整数,并打印那些严格包含在n中的整数,即:(i & n) == i。
void list_descendants(int n) {
printf("descendants of %d:", n);
for (int i = n; i --> 1;) {
if ((i & n) == i)
printf(" %d", i);
}
printf("\n");
}发布于 2021-01-19 16:28:35
好的,所以我终于想出了一个C语言的代码,这个代码看起来很不好看,而且可能经过了可怕的优化,但仍然按照预期工作。可能有更简单的解决方案,但为了知识目的,这里是我的解决方案:
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
unsigned int pui(unsigned int a, unsigned int b)
{
if (b == 0)
return 1;
if (b == 1)
return a;
if (b % 2 == 0)
return pui(a * a, b / 2);
else
return pui(a * a, (b - 1) / 2);
}
unsigned int testBit(unsigned int h, unsigned int offset)
{
unsigned int mask = 1 << offset;
return (h & mask);
}
bool isInList(unsigned int x, unsigned int *output_size, unsigned int **output)
{
for (int i = 0; i < *output_size; i++)
{
if (*(*output + i) == x)
{
return true;
}
}
return false;
}
void listDescendants(unsigned int h, unsigned int *output_size, unsigned int **output, int *currently_processing)
{
unsigned int max_offset = 0;
unsigned int temp_h = h;
unsigned int initial_output_size = *output_size;
while (temp_h > 0)
{
max_offset++;
temp_h /= 2;
}
unsigned int h_radix2[max_offset];
for (int i = 0; i < max_offset; i++)
{
if (testBit(h, i))
{
if (h > pui(2, i) && !isInList(h - pui(2, i), output_size, output))
{
*(*output + *output_size) = h - pui(2, i);
*output_size += 1;
}
}
}
if (*currently_processing < (int)*output_size)
{
*currently_processing += 1;
listDescendants(*(*output + *currently_processing), output_size, output, currently_processing);
}
}
int main()
{
int currently_processing = -1;
unsigned int size = 0;
unsigned int *output = malloc(300 * sizeof(unsigned int));
listDescendants(21, &size, &output, ¤tly_processing);
printf("size = %u\n", size);
for (int i = 0; i < size; i++)
{
printf("%u ", output[i]);
}
printf("\n");
return 0;
}发布于 2021-01-19 20:43:11
您可以使用递归解决方案,如下面的解决方案。
我有点懒,所以我没有把数字放在列表中,而是简单地打印出来,我还打印了0和给定的数字。
我相信你可以很容易地调整代码,这样它就能做你想做的事情。
#include <stdio.h>
#define CHAR_BIT 8
void ProperDescendantsRecursive(unsigned int num, unsigned int bit_index)
{
if (CHAR_BIT * sizeof(unsigned int) - 1 == bit_index)
{
printf("%u\n", num);
}
else
{
unsigned int mask = 1U << bit_index;
if (num & mask)
{
/* with the bit at index bit_index is off */
ProperDescendantsRecursive(num ^ mask, bit_index + 1);
/* with the bit at index bit_index is on */
ProperDescendantsRecursive(num, bit_index + 1);
}
else
{
ProperDescendantsRecursive(num, bit_index + 1);
}
}
}
void ProperDescendants(unsigned int num)
{
ProperDescendantsRecursive(num, 0);
}
int main(void)
{
ProperDescendants(21);
return 0;
}编译和运行结果如下:
0
16
4
20
1
17
5
21https://stackoverflow.com/questions/65793351
复制相似问题