Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 向上计数并以二进制打印输入的整数_C_Recursion_Binary - Fatal编程技术网

C 向上计数并以二进制打印输入的整数

C 向上计数并以二进制打印输入的整数,c,recursion,binary,C,Recursion,Binary,我正在尝试打印输入的整数的二进制,从0开始计数(例如,0,1,10,11100…到输入的数字)。我已经完成了大部分编码。我只是在努力处理打印二进制代码中的两个递归调用:它一次打印出每个数字1或0,直到需要转到下一行。我想知道是否有人可以查看我的代码并告诉我应该做什么? 代码: #包括 int biggestPower2(int); 无效打印二进制文件(int,int); int main() { int i; int输入; printf(“输入要计数的数字:”); scanf(“%d”,输入(&

我正在尝试打印输入的整数的二进制,从0开始计数(例如,0,1,10,11100…到输入的数字)。我已经完成了大部分编码。我只是在努力处理打印二进制代码中的两个递归调用:它一次打印出每个数字1或0,直到需要转到下一行。我想知道是否有人可以查看我的代码并告诉我应该做什么? 代码:

#包括
int biggestPower2(int);
无效打印二进制文件(int,int);
int main()
{
int i;
int输入;
printf(“输入要计数的数字:”);
scanf(“%d”,输入(&I));

对于(i=0;i而言,
printBinary
函数的正确表示应如下所示:

printBinary(a,b) = if(b==1) print a
                   if(a>b) print "1" 
                   else print "0"
                   printBinary(a%b, b/2)

where the initial b is largest power of 2 less than equal to a.                   
void printBinary(int decimal, int power2)
{
  if (power2 == 1)
  {
      printf("%d\n", decimal);
      return;
  }
  printf("%d", decimal/power2);
  printBinary(decimal%power2, power2/2);
}
void printBinary(int decimal)
{
    if(decimal<=1)
      printf("%d", decimal);
    else
    {
      printBinary(decimal/2);
      printf("%d", decimal&1);
    }
}
您从小于给定数字的最高功率开始。如果它小于当前数字,则设置位,否则不设置位。然后用剩余的
a%b
和下一个功率
b/2
调用相同的函数

修改后的代码应如下所示:

printBinary(a,b) = if(b==1) print a
                   if(a>b) print "1" 
                   else print "0"
                   printBinary(a%b, b/2)

where the initial b is largest power of 2 less than equal to a.                   
void printBinary(int decimal, int power2)
{
  if (power2 == 1)
  {
      printf("%d\n", decimal);
      return;
  }
  printf("%d", decimal/power2);
  printBinary(decimal%power2, power2/2);
}
void printBinary(int decimal)
{
    if(decimal<=1)
      printf("%d", decimal);
    else
    {
      printBinary(decimal/2);
      printf("%d", decimal&1);
    }
}
顺便说一句,要找到
2
的最大幂小于或等于
n
,只需执行以下操作:

 long int largest_powr_less_than_n = pow(2.0,floor(log(n)/log(2)))
而不是编写一个单独的(太递归了!)函数

编辑:

更简单的递归实现如下所示:

printBinary(a,b) = if(b==1) print a
                   if(a>b) print "1" 
                   else print "0"
                   printBinary(a%b, b/2)

where the initial b is largest power of 2 less than equal to a.                   
void printBinary(int decimal, int power2)
{
  if (power2 == 1)
  {
      printf("%d\n", decimal);
      return;
  }
  printf("%d", decimal/power2);
  printBinary(decimal%power2, power2/2);
}
void printBinary(int decimal)
{
    if(decimal<=1)
      printf("%d", decimal);
    else
    {
      printBinary(decimal/2);
      printf("%d", decimal&1);
    }
}
void printBinary(整数十进制)
{
如果(十进制更简单:

void printBinary(int decimal, int power2)
{
  int nextDigit;
  if (power2 == 1)
  {
      printf("%d\n", decimal);
  }
  else
  {
      nextDigit = decimal/power2;
      printf("%d", nextDigit);
      printBinary(decimal%power2, power2/2);
  }
}

你进入了一个无限递归。使用调试器,一步一步地执行你的程序,你就会明白为什么。顺便说一句,这里没有理由使用递归。它不是无限递归,只是数字不正确(除非我错误地将它复制到这里),我也知道不需要,但这是我的课程所要求的,实际上你是对的,这不是无限递归,整个算法都是错误的。