简体   繁体   English

斐波那契函数的问题。 C ++

[英]Problem with fibonacci function. C++

Should return the n place of the array. 应该返回数组的n位。 But instead of the value I'm only getting 0. 但不是价值,我只得到0。

int fibonacci(int n)
{
    int f[100];
    f[0] = 0;
    f[1] = 1;

    for (int i=2; i<n; i++)
    {
        f[i] = f[i-2] + f[i-1];
    }

    return f[n];
}

int main()
{
    cout << fibonacci(3);
    return 0;
}

New CODE: 新代码:

New problem its returning one number further then it should. 新问题,它应该进一步返回一个数字。 For example if 'n==7' its returning '13' not '8' like it should. 例如,如果'n == 7',则返回'13'而不是'8'。

int fibonacci(int n)
{
    int f[100] = { 0, 1 };

    for (int i=2; i<=n; i++)
    {
        f[i] = f[i-2] + f[i-1];
    }

    return f[n-1];
}

int main()
{
    cout << fibonacci(7);
    return 0;
}

Your loop termination condition is wrong. 您的循环终止条件错误。 Since this is homework perhaps you can work out why. 既然这是家庭作业,也许你可以找出原因。

well, you never set f[n] , you only go up to i < n , that is, i == n-1 . 好吧,你永远不会设置f[n] ,你只能达到i < n ,即i == n-1 try returning f[n-1] 尝试返回f[n-1]

EDIT: as Chris Lutz pointed out my answer is no good as it would give an invalid result if you called fibonacci(0) 编辑:正如Chris Lutz所指出的那样,我的回答并不好,因为如果你打电话给fibonacci(0)会产生无效的结果

Like many have answered already, the best solution is to loop until i <= n 像许多人已经回答的那样,最好的解决方案是循环直到i <= n
Unless, of course, you want fibonacci(3) to return the 3rd element in the fibonacci sequence and not the 4th, in which case fibonacci(0) wouldn't really make sense, and the right return value would be f[n-1] ... still the n==0 case should be handled somehow, as should the n<0 and the n>100 cases. 当然,除非你想让fibonacci(3)返回斐波那契序列中的第三个元素而不是第四个元素,在这种情况下, fibonacci(0)不会真正有意义,正确的返回值将是f[n-1] ......仍然应该以某种方式处理n==0情况, n<0n>100情况也应如此。

you can return f[n-1] as long as you check for the right boundaries: 只要检查正确的边界,就可以返回f[n-1]

int fibonacci(int n)
{
    int f[100] = { 0, 1 };

    if ((n <= 0) || (n > 100))
        return -1;//return some invalid number to tell the caller that he used bad input

    for (int i=2; i < n; i++) // you can use i < n here
    {
        f[i] = f[i-2] + f[i-1];
    }

    return f[n-1];
}

You forgot to initialize the n-th value of the array. 你忘了初始化数组的第n个值。 You return f[n] but only initialize up to n-1. 您返回f [n]但仅初始化为n-1。

The trouble is that you test for i < n (where n == 3 in your example call), but you return f[3] which has not been set to anything. 麻烦的是你测试i < n (在你的示例调用中n == 3 ),但你返回f[3] ,但没有设置为任何东西。 You are 'lucky' that you're getting zeroes rather than random garbage. 你很幸运,你得到零而不是随机垃圾。

Change the ' < ' to ' <= '. 将' < '更改为' <= '。


Working Code #1 工作守则#1

Retaining the full size array. 保留全尺寸阵列。

#include <iostream>
using namespace std;

static int fibonacci(int n)
{
    int f[100] = { 0, 1 };

    if (n < 0 || n > 100)
        return -1;
    else if (n < 2)
        return f[n];

    for (int i = 2; i <= n; i++)
    {
        f[i] = f[i-2] + f[i-1];
        //cout << "f[" << i << "] = " << f[i] << endl;
    }

    return f[n];
}

int main()
{
    for (int i = 0; i < 8; i++)
        cout << "fib(" << i << ") = " << fibonacci(i) << endl;
    return 0;
}

Sample Output #1 样本输出#1

fib(0) = 0
fib(1) = 1
fib(2) = 1
fib(3) = 2
fib(4) = 3
fib(5) = 5
fib(6) = 8
fib(7) = 13

Working Code #2 工作守则#2

This uses an array of size 3, at the cost of a lot of modulo operations: 这使用大小为3的数组,代价是模数运算很多:

#include <iostream>
using namespace std;

static int fibonacci(int n)
{
    int f[3] = { 0, 1, 0 };

    if (n < 0 || n > 100)
        return -1;
    else if (n < 2)
        return f[n];

    for (int i = 2; i <= n; i++)
    {
        f[i%3] = f[(i-2)%3] + f[(i-1)%3];
        //cout << "f[" << i << "] = " << f[i%3] << endl;
    }

    return f[n%3];
}

int main()
{
    for (int i = 0; i < 8; i++)
        cout << "fib(" << i << ") = " << fibonacci(i) << endl;
    return 0;
}

It produces the same output - so there is no point in repeating it. 它产生相同的输出 - 因此重复它没有意义。

Working Code #3 工作守则#3

Avoiding arrays and modulo operations: 避免数组和模运算:

#include <iostream>
using namespace std;

static int fibonacci(int n)
{
    int f0 = 0;
    int f1 = 1;

    if (n < 0 || n > 46)
        return -1;
    else if (n == 0)
        return f0;
    else if (n == 1)
        return f1;

    int fn;
    for (int i = 2; i <= n; i++)
    {
        int fn = f0 + f1;
        f0 = f1;
        f1 = fn;
        //cout << "f[" << i << "] = " << fn << endl;
    }

    return f1;
}

int main()
{
    for (int i = -2; i < 50; i++)
        cout << "fib(" << i << ") = " << fibonacci(i) << endl;
    return 0;
}

The limit 46 is empirically determined as correct for 32-bit signed integers. 根据经验确定限制46对于32位有符号整数是正确的。

Example Output #3 示例输出#3

fib(-2) = -1
fib(-1) = -1
fib(0) = 0
fib(1) = 1
fib(2) = 1
fib(3) = 2
fib(4) = 3
fib(5) = 5
fib(6) = 8
fib(7) = 13
fib(8) = 21
fib(9) = 34
fib(10) = 55
fib(11) = 89
fib(12) = 144
fib(13) = 233
fib(14) = 377
fib(15) = 610
fib(16) = 987
fib(17) = 1597
fib(18) = 2584
fib(19) = 4181
fib(20) = 6765
fib(21) = 10946
fib(22) = 17711
fib(23) = 28657
fib(24) = 46368
fib(25) = 75025
fib(26) = 121393
fib(27) = 196418
fib(28) = 317811
fib(29) = 514229
fib(30) = 832040
fib(31) = 1346269
fib(32) = 2178309
fib(33) = 3524578
fib(34) = 5702887
fib(35) = 9227465
fib(36) = 14930352
fib(37) = 24157817
fib(38) = 39088169
fib(39) = 63245986
fib(40) = 102334155
fib(41) = 165580141
fib(42) = 267914296
fib(43) = 433494437
fib(44) = 701408733
fib(45) = 1134903170
fib(46) = 1836311903
fib(47) = -1
fib(48) = -1
fib(49) = -1

n is the index that is never reached in your version. n是您的版本中从未达到的索引。 You just need to replace the < with <= in your for loop conditional. 您只需要在for循环条件中替换< with <= (You never assigned f[n] because n was never reached by the loop and so you got back a default value.) (你从未指定f [n],因为循环从未到达过n,所以你得到了一个默认值。)

int fibonacci(int n)
{
    int f[100];
    f[0] = 0;
    f[1] = 1;

    for (int i=2; i<=n; i++)
    {
        f[i] = f[i-2] + f[i-1];
    }

    return f[n];
}

int main()
{
    cout << fibonacci(3);
    return 0;
}

And you don't need an array to perform the fib sequence by the way. 而且你不需要一个数组来执行fib序列。 Just use two variables and reassign them in the loop. 只需使用两个变量并在循环中重新分配它们。 Something like this: 像这样的东西:

int a = 0;
int b = 1;

for (int i=2; i<=n; i++)
{
    b = a + b;
    a = b;
}

return b;

With the call fibonacci(3) , your for loop (inside the fibonacci function) goes until i < 3 ... 通过调用fibonacci(3) ,你的for循环(在fibonacci函数内)直到i < 3 ...

It means that the last assigment is f[2] . 这意味着最后一个分配是f[2] Not f[3] as expected (which is the value you return). 不是预期的f[3] (这是你返回的值)。

Don't you mean return f[n-1]; 你不是说返回f [n-1];

I guess your compiler has set the array f[100] to 0? 我猜你的编译器已经将数组f [100]设置为0?

Looks like the other guy has the right answer.... 看起来其他人有正确的答案....

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

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