【问题标题】:c program to generate binary number starting from 1 till nc程序生成从1到n的二进制数
【发布时间】:2021-12-14 20:59:21
【问题描述】:

我需要一个输出,比如如果数字为 5,则输出应该是 5 个二进制数,直到 5,但从 1 开始,下面给出了示例。无需使用数组即可完成

输入:5 输出:1,10,11,100,101

但我得到了 输出像 这 输入=5 输出=1,11,11,111,111

这个错误是因为错误的指令 任何提示或解决方案

#include <stdio.h>
#include <math.h>
void binary_number(int n)
{

    int bin_n = 0;

    int i = 0;

    while (n > 0)
    {

        bin_n = n % 2;

        n = n / 2;

        i++;

    }
    for (int j = i - 1; j >= 0; j--)

        printf("%d", bin_n, j);

    printf("\n");
}
void binary_number_generation(int n)
{
    for (int i = 0; i <= n; i++)

    {
        binary_number(i);
    }
}
int main()
{
    int n;

    printf("Enter the end value:");

    scanf("%d", &n);


    printf("\nBinary Number from 1 to %d:", n);

    printf("\n");

    binary_number_generation(n);

}

这里我有一个新代码 idk 我做了什么,但它确实给出了结果

#include <stdio.h>
int main()
{
    int n,i=0,bin=0,plc,dig=0,j;
    printf("Enter the n value : ");
    scanf("%d",&n);
    printf("\nBinary numbers from 1 to %d : ",n);
    for(i=0;i<=n;i++)
    {
        plc=0;bin=0;dig=0;
        for(plc=1,j=i;j>0;j=j/2)
        {
            dig=j%2;
            bin=bin+(dig*plc);
            plc=plc*10;
        }
        printf("%d\n",bin);
    }
}

【问题讨论】:

  • 这不是你的错,但是:这是一个几乎不可能的问题。在过去的几周里,我们已经看到了它的几种变化(也许是你的同学,在同一个虐待狂导师的指导下?)。
  • @SteveSummit 引用的“几乎不可能”的部分是以正确的顺序获取位(例如 100 而不是 4 的 001)。使用后遍历打印递归就可以了。
  • 您可能无法一下子解决所有问题。我建议首先解决以下两个更简单的问题之一: (1) 打印前 N 个二进制数,但您允许使用数组。 (2) 打印前 N 个二进制数,其中每个数都有其数字向后。也就是说,4 是001,5 是101,6 是011,8 是0001,10 是0101,等等。一旦你有一个工作,那么你可以解决你的实际问题。我正要给你同样的提示 ikegami 做的:递归。
  • 您发布的代码不起作用的原因基本上是bin_n 不是数组。如果它是一个适当索引的数组,你的代码就可以正常工作。
  • @SteveSummit ik 对于像我这样的初学者来说,至少没有数组是不可能的,希望有人能提供解决方案:(

标签: c visual-studio binary logic decimal


【解决方案1】:

我们以 6 为例。

610 = 1 * 22 + 1 * 21 + 0 * 20 = 1102

通常,在构建二进制数时,您会使用数组。但我们不能。但是,如果我们可以将 110 base 2(六)转换为数字 110 base 10(一百一十)。

11010 = 1 * 102 + 1 * 101 + 0 * 100

看起来可行!

6 base 10 = 1 * 2^2  + 1 * 2^1  + 0 * 2^0  = 110 base 2

            |          |          |
            v          v          v

            1 * 10^2 + 1 * 10^1 + 0 * 10^0 = 110 base 10

我们将这种格式称为十进制编码二进制(引用binary-coded decimal)。这将允许我们做的是使用printf %d 或类似的东西来打印号码。

解决方案如下。在继续阅读之前,您应该尝试自己实现它。


#include <inttypes.h>
#include <stdio.h>
#include <stdint.h>

// Converts XYZ (base 2) to XYZ (base 10)
// 8 bit inputs require ceil(log2(11111111)) = 24 bit outputs.
// 16 bit inputs require ceil(log2(1111111111111111)) = 50 bit outputs.
uint64_t to_dcb(uint16_t n) {
   uint64_t dcb = 0
   uint64_t factor = 1;
   while (n > 0) {
      dcb += (n & 1) * factor;
      n >>= 1;
      factor *= 10;
   }

   return dcb;
}

int main(void) {
   printf("%" PRIu64 "\n", to_dcb(6));
   return 0;
}

【讨论】:

  • 我喜欢你的术语“十进制编码的二进制”。另请参阅this answer,了解如何处理高达 32 位的输入。
【解决方案2】:

据我了解,您的代码即将将每个数字从 1 转换为 n 为二进制形式

#include <stdio.h>
#include <math.h>

/* This function is to convert the number of digits and then calculate the rest of division 
  if it's 1 or 0 and then we will form the new number in binary form */

int binary_number(n)
{
    int m=0,q=1,s=0;
    while(n!=0)
    {
        s+=(n%2)*q;
        q*=10;
        n/=2;
    }
    return s;
}

/* This number is using to display each number from 1 to n in binary form, 
   it's better to use an array in next time */         

void binary_number_generation(int n)
{
    for (int i = 0; i <= n; i++)
    {
        printf("\n%d\n",binary_number(i));
    }
}


int main()
{
    int n;
    printf("Enter the end value:");
    scanf("%d",&n);
    binary_number_generation(n);
    printf("\n");
}

【讨论】:

    【解决方案3】:

    这是另一种生成整数二进制表示的方法。它有几个问题,但是,它的优点是它以从左到右的顺序生成数字,因此您可以立即将它们打印出来,而不必将它们存储在数组中。

    void binary_number(int n)
    {
        int big = 32768;
        while(big > 0) {
            if(n >= big) {
                putchar('1');
                n -= big;
            } else {
                putchar('0');
            }
    
            big /= 2;
        }
    }
    

    学生练习:修改它,使其不打印前导 0。

    练习 #2:如果你在大于 65535 的数字上尝试这个函数会发生什么?你能解决它吗?

    【讨论】:

      猜你喜欢
      • 2021-12-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-06
      • 1970-01-01
      • 2014-11-30
      • 1970-01-01
      • 2013-08-19
      相关资源
      最近更新 更多