繁体   English   中英

斐波那契函数的递归实现

[英]Recursive implementation of Fibonacci function

我已经编写了代码来查找斐波那契数和调用的次数,以在 C 中找到相同的递归版本。我无法删除编译错误。 请帮忙。

代码如下:

#include <stdio.h>

int main(int fib) {
  int n,m, count=0; //'count' counts #times function is called
  printf("enter n");
  scanf("%d",&n);
  return fib_rec(n, &count);
}

int fib_rec(int n, int *count)
{
  int b=0,c=1;
  *count = *count +1;
  if(n<=1)
  { 
    return n;
  }
  else
  {
    printf (count);
    return fib_rec(n-1, *count)+ fib_rec(n-2, *count);
  }
}

在 reptl.it 站点上运行时,编译错误如下所示,如下所示。

main.c: In function 'main':
main.c:7:10: warning: implicit declaration of function 'fib_rec' [- 
Wimplicit-function-declaration]
   return fib_rec(n, &count);
      ^~~~~~~
main.c: In function 'fib_rec':
main.c:20:13: warning: passing argument 1 of 'printf' from incompatible 
pointer type [-Wincompatible-pointer-types]
   printf (count);
         ^~~~~
In file included from main.c:1:
/usr/include/stdio.h:364:43: note: expected 'const char * restrict' but 
argument is of type 'int *'
   extern int printf (const char *__restrict __format, ...);
                ~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
main.c:21:25: warning: passing argument 2 of 'fib_rec' makes pointer from 
integer without a cast [-Wint-conversion]
   return fib_rec(n-1, *count)+ fib_rec(n-2, *count);
                     ^~~~~~
main.c:10:25: note: expected 'int *' but argument is of type 'int'
   int fib_rec(int n, int *count)
                ~~~~~^~~~~
main.c:21:47: warning: passing argument 2 of 'fib_rec' makes pointer from 
integer without a cast [-Wint-conversion]
   return fib_rec(n-1, *count)+ fib_rec(n-2, *count);
                                           ^~~~~~
main.c:10:25: note: expected 'int *' but argument is of type 'int'
  int fib_rec(int n, int *count)
                ~~~~~^~~~~
enter n 10
exit status -1
>

我将您的代码粘贴到https://tio.run/#c-gcc 上,结果如下:

main原型

.code.tio.c:3:7: warning: ‘main’ takes only zero or two arguments [-Wmain]
   int main(int fib) {
       ^~~~

main函数原型是int main(void)int main(int argc, char *argv[])

由于您希望用户输入一个数字,因此您可以选择第一种形式。

count类型

.code.tio.c: In function ‘main’:
.code.tio.c:7:5: error: ‘count’ undeclared (first use in this function)
     count = 0;  // counts the number of times the function is called
     ^~~~~

你必须给一个类型来count变量,比如

int count = 0;

fib_rec声明

.code.tio.c:8:12: warning: implicit declaration of function ‘fib_rec’ [-Wimplicit-function-declaration]
     return fib_rec(n, &count);
            ^~~~~~~

在使用该函数之前,您尚未声明该函数。

你可以这样声明: int fib_rec(int n, int *count)例如在main定义之前。

printf用法

.code.tio.c: In function ‘fib_rec’:
.code.tio.c:21:15: warning: passing argument 1 of ‘printf’ from incompatible pointer type [-Wincompatible-pointer-types]
       printf (count);
               ^~~~~

printf函数要求进行一些格式化。 如果要显示整数值,请使用%d

printf("count value is: %d\n", count);

不兼容的指针类型

.code.tio.c:22:27: warning: passing argument 2 of ‘fib_rec’ makes pointer from integer without a cast [-Wint-conversion]
       return fib_rec(n-1, *count)+ fib_rec(n-2, *count);
                           ^~~~~~

这里count已经是一个整数指针, *是不必要的:

return fib_rec(n-1, count)+ fib_rec(n-2, count);

计算值的显示

您的代码返回计算值,但不显示它。 为此,请替换return fib_rec(n, &count);

printf("fib_rec(%d) = %d\n", n, fib_rec(n, &count));
return 0;

所以更正后的代码可能是:

#include <stdio.h>

int fib_rec(int n, int *count);

int main(void) {
    int n;
    printf("enter n\n");
    scanf("%d",&n);
    int count = 0;  // counts the number of times the function is called
    printf("fib_rec(%d) = %d\n", n, fib_rec(n, &count));
    return 0;

}

int fib_rec(int n, int *count)
{
    int b=0,c=1;
    *count = *count +1;
    if(n<=1)
    { 
       return n;
    }
    else
    {
      printf ("count: %d\n", *count);
      return fib_rec(n-1, count)+ fib_rec(n-2, count);
    }
}

main

  1. main有一个int main(void)int main(int argc, int argc *[])的原型。 fib不是main的有效输入。

  2. main通常会返回0 所以这行fib_rec(n, &count); 应该放在返回之前。

  3. 变量count未声明。 变量m未使用。

  4. main函数之前, fib_rec的声明应该在那里。 int fib_rec(int n, int *count);

在 fib_rec

  1. printf不正确。 - 它应该是带有*count %d

  2. 递归调用是不对的,由于fib_rec中的count是一个指针,可以不取值直接再次传给函数。 像这样 - return (fib_rec(n-1, count)+ fib_rec(n-2, count));

  3. 未使用的变量bc

这解决了编译问题。 代码如下。

int fib_rec(int n, int *count);

int main(void) {

  int n;
  int count;
  printf("enter n"); 
  scanf("%d",&n);
  count = 0;  // counts the number of times the function is called
  fib_rec(n, &count);
  return 0;

}

int fib_rec(int n, int *count)
{
   *count = *count +1;

   if ((n<=1) )
   { 
      return 1;
   }
   else
   {
     printf ("%d ", n);
     return (fib_rec(n-1, count)+ fib_rec(n-2, count));
   }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM