C语言 查找整数的正确后代[已关闭]

r7knjye2  于 2023-03-11  发布在  其他
关注(0)|答案(3)|浏览(120)

**已关闭。**此问题需要debugging details。当前不接受答案。

编辑问题以包含desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将有助于其他人回答问题。
两年前关闭了。
Improve this question
我陷入了一个基本的算法问题,我不知道如何解决它。
基本上,我想列出一个整数的所有后代,也就是说,如果我的数字是21,我想使用它的二进制表示(10101),并列出所有至少有一个公共位的值为1的数字,以及小于21的数字,这里的结果应该是10100,10001,10000,101,100,1。
真后代的数学定义如下:

  • 设h为小于2^m的非负数,其中di = 0或1。
  • 设h'为另一个非负的,如h' = d0' + d1'*2^1 + ... + dm-1'*2^(m-1),其中di' = 0或1。
  • h'是h的后代,如果di'〈=di,且0〈=i

我尝试过很多Python和C语言的实现,也尝试过传统的纸笔技术,但都失败了。我知道这很简单,但我似乎搞不懂。我是用C语言编写代码的,所以如果你能找到一个用C语言工作的解决方案,那将是理想的,但我现在愿意接受任何东西。

gopyfrb3

gopyfrb31#

下面是一个非常简单的方法:枚举n-11之间的所有整数,并打印严格包含在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");
}
anauzrmj

anauzrmj2#

好吧,我终于想出了一个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, &currently_processing);

  printf("size = %u\n", size);
  for (int i = 0; i < size; i++)
  {
    printf("%u ", output[i]);
  }
  printf("\n");

  return 0;
}
bqucvtff

bqucvtff3#

您可以使用递归解决方案,如下所示。
我有点懒,所以我没有把数字列成一个列表,而是简单地打印出来,我还打印了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
21

相关问题