解决斐波那契数列在函数中递归返回void

发布于 2024-09-24 18:24:20 字数 415 浏览 7 评论 0原文

我的教授要求我们编写一个程序,使用递归来解决斐波那契数列。这很正常,但他要求我们让函数返回 void。我已经为此工作了几天,但找不到方法来做到这一点。

我:

void fibonacci(double *n,double *x,double *y,double *result) {
     if(*n == 1)
          *result = 0;
     else if(*n == 2)
          *result = 1;
     else
          fibonacci(--n,n,(n-1),(n+(n-1))); }

我现在做的事对吗?我从来没有用过 以前以这种方式参数,我不确定 如果我走在正确的轨道上。由于某种原因它是 不在斐波那契递归调用时进行编译, 说明指针加法无效。谢谢!

My professor has asked us to write a program that uses recursion to solve a fibonacci sequence. This is all pretty normal, but he's asked us to make our function return void. I've been working at this for a few days now and can't find a way to do this.

I have:

void fibonacci(double *n,double *x,double *y,double *result) {
     if(*n == 1)
          *result = 0;
     else if(*n == 2)
          *result = 1;
     else
          fibonacci(--n,n,(n-1),(n+(n-1))); }

Is what I'm doing right? I have never had to use
parameters in such ways before and I'm not sure
if I'm on the right track. For some reason it's
not compiling at the recursive call to fibonacci,
stating invalid pointer addition. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

零度° 2024-10-01 18:24:20

提示:问题就在那里: fibonacci(--n,n,(n-1),(n+(n-1))); 甚至就在那里 --n。您正在使用指针

Hint: problem is there: fibonacci(--n,n,(n-1),(n+(n-1))); or even just there --n. You're working with pointers

甜柠檬 2024-10-01 18:24:20

编译器是对的。如果使用指针,则需要在调用中取消引用指针。

但更简单的解决方案是使用此原型(并将所有代码与其匹配):

void fibonacci(int n, int *result).
  • 我已将 double 替换为 int,因为我不明白为什么要使用 double 来存储整数。
  • 我删除了您在函数中不使用的 x 和 y 。

The compiler is right. You need to dereference the pointers in the call, if you use pointers.

But the simpler solution would be to use this prototype instead (and match all code to it) :

void fibonacci(int n, int *result).
  • I've replaced double by int, because I don't see why you'd use double to store integers.
  • I've removed x and y which you don't use in your function.
神妖 2024-10-01 18:24:20

由于这是一项作业,我不会提供工作代码,尽管这里有几点:

  1. 使用引用比使用指针更简单
  2. 您确实需要增加结果,而不是将其设置为 0 或 1。因此您需要传递给第一个函数通过引用调用一个赋值为 0 的 int
  3. 。考虑公式: f(n) = f(n-1) + f(n-2) 对于所有 n > 。 2;当 n=1 时,f(n) = 0;当 n=2 时,f(n) = 1。

Since this is a homework, I won't provide working code, although a few points here:

  1. Using a reference is simpler than using pointers
  2. You really need to increase the result, not set it to 0 or 1. Therefore you need to pass to first function call by reference an int with assigned value of 0.
  3. Consider the formula: f(n) = f(n-1) + f(n-2) for all n > 2; f(n) = 0 for n=1 and f(n) = 1 for n=2.
栩栩如生 2024-10-01 18:24:20

不,不是。
首先,您要减去指向浮点的指针(在 --n 处),这可能很容易(即使您编译并运行它)产生访问冲突。它正确地抱怨了类型。该函数接受的类型是指针,我敢打赌您正在传递浮点数。

no it is not.
1st of all you are subtracting pointers to float (at --n) which might easily (even if you compile it and run it) produce access violation. It correctly complains though about types. The types that the function accepts are pointers and I bet you are passing floats.

戈亓 2024-10-01 18:24:20

使用此作为开始:

void fibonacci(double n, double & result) {
    if(n == 1)
        result = 0;
    else if(n == 2)
        result = 1;
    else {
        // gotta figure that part out yourself
    }
}

通过将 result 声明为引用,您的修改将更改传递的实际参数的值。由于这是 C++ 参考,因此应该优先考虑。您仍然可以将 n 声明为普通值,因为您不想修改它。递归调用现在就是你的作业了:)

Use this for a start:

void fibonacci(double n, double & result) {
    if(n == 1)
        result = 0;
    else if(n == 2)
        result = 1;
    else {
        // gotta figure that part out yourself
    }
}

By declaring result as a reference, your modification will change the value of the actual parameter passed. Since this is C++ references should be preferred. You can still declare n as a normal value, because you do not want to modify it. The recursive call is your homework now :)

电影里的梦 2024-10-01 18:24:20

我想一定是这样的:

void fibonacci_list()
{
   int count,next=1,prev1=0,prev2;
   printf("1");
   for(count=2;count<=12;count++)
   {
       prev2=prev1;
       prev1=next;
       next=prev1+prev2;
       printf("%d ",next);
   }
   printf("...");
   return;
}

I think it must be like this:

void fibonacci_list()
{
   int count,next=1,prev1=0,prev2;
   printf("1");
   for(count=2;count<=12;count++)
   {
       prev2=prev1;
       prev1=next;
       next=prev1+prev2;
       printf("%d ",next);
   }
   printf("...");
   return;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文