【问题标题】:Fibonacci Function with memoization in C++ [duplicate]C ++中带有记忆的斐波那契函数[重复]
【发布时间】:2021-12-22 00:45:57
【问题描述】:

我试图在 C++ 中创建一个带有记忆功能的斐波那契函数。我选择了以下方法。

#include <iostream>
using namespace std;


int fibonacci(int index = 0) // Recursive // SwitchCase
{
    switch (index)
    {
    case 0:
        return 0;
    case 1:
        return 1;
    default:
        return fibonacci(index - 2) + fibonacci(index - 1);
    }
}

int fibo(int i) // Recursive & Memoisation
{
    static int const maxIndex = 2000;
    static int seq[maxIndex] = {0, 1};
    static int count = 2;
    if(i < count){return seq[i];}
    int temp = fibo(i-2) + fibo(i-1);
    count = count + 1;
    seq[i] = temp;
    return temp;
}

int main()
{
    int n = 50;
    for (int i=0; i<=n; i++)
    {cout << i << ":\t:" << fibo(i) << endl;} // Memoization
    cout << "\n\n" << endl;
    for (int i=0; i<=n; i++)
    {cout << i << ":\t:" << fibonacci(i) << endl;} // Non Memoization
    return 0;
}

我从索引 47 中注意到一些奇怪的东西。输出如下:

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


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

附:我做了非记忆化方法只是为了检查数组部分是否有问题。

我不明白为什么输出包含 整数。 我也尝试使用double 而不是int,但输出仍然有数字。

谁能解释一下为什么会这样? 提前致谢

【问题讨论】:

  • 由于int 范围溢出。
  • int 不能容纳任意大的数字。查看std::numeric_limits&lt;int&gt;::max()的值是多少,然后选择更宽的类型
  • 使用double的代码在哪里?

标签: c++ arrays fibonacci memoization memoise


【解决方案1】:

所以基本上 int 有一个限制值,范围是从 -21474836472147483647

如果你想写更长的数字,你应该考虑使用:

  • unsigned int:具有最大值4294967295 只有正数
  • long long:有最大值9223372036854775807
  • unsigned long long:有最大值18446744073709551615 只有正数

还有一个解决方案:

你可以把很长的数分成几ints,然后输出一行。

【讨论】:

  • 范围由实现定义。
猜你喜欢
  • 2020-08-19
  • 1970-01-01
  • 2021-11-17
  • 1970-01-01
  • 2021-05-21
  • 1970-01-01
  • 2019-06-04
  • 2015-10-19
  • 2011-12-14
相关资源
最近更新 更多