什么是 int numbers[n+2];声明呢?

what does int numbers[n+2]; statement do?

  #include<iostream>

    int fastFibonacci(int n) 
    {
      int numbers[n+2]; // int numbers[n].
      numbers[0] = 0;
      numbers[1] = 1;
      for (int i = 2; i <= n; i++)
      {
          numbers[i] = numbers[i - 1] + numbers[i - 2];
      }
      return numbers[n];
    }

    int main() {
      int n;
      std::cout << "Enter a Number";
      std::cin >> n;
      int result = fastFibonacci(n);
      std::cout << result << "\n";
      return 0;
    }

在这段代码中,当我输入 0 或 1 时得到正确答案。但问题是,当我用注释部分替换 int numbers[n+2]; 时,当输入为 0 或 1 时,它开始给我错误的答案。为什么?任何人请解释我。

int numbers[n+2];int 数组的声明,其中 space for n + 2 ints, this is a variable lenght array and is not part of C++ standard,尽管有些编译器允许它,但您不应该使用它。

如果您需要可变长度数组,请使用 std::vector.

使用 int numbers[n+2]; 如果 n 等于 0 你仍然有 space 2 ints,如果你有 int numbers[n]; 数组将有space 0 ints,所以代码将失败,因为您试图访问 numbers[0]numbers[1].

不存在的内存

有几种实现斐波那契数列的好方法,在网站上您可以找到许多关于这个问题的几种编程语言的问题,这里是其中之一 Fibonacci series in C++

编辑

所以我看到了你关于使用向量的评论,为了制作序列你不需要向量只需要两个变量来存储要添加的两个数字,将序列存储在 vactor 中,你可以这样做类似于:

#include <iostream>
#include <vector>
#include <iomanip>

//passing the vector by reference
void fastFibonacci(unsigned long long n, std::vector<unsigned long long>& sequence) {

   unsigned long long first = 0;
   unsigned long long second = 1;
   sequence.push_back(first); //add first values to the vector
   sequence.push_back(second); //add first values to the vector
   for (unsigned long long i = 0, value = 0; i < n && value <= LLONG_MAX ; ++i) {
      value = first + second;
      first = second;
      second = value;
      sequence.push_back(value); //adding values to the vector
   }  
}

int main() {  
   unsigned long long limit; //number of values in the sequence
   int num = 1;
   std::vector<unsigned long long> sequence; //container for the sequence

   std::cout << "Enter upper limit: ";
   std::cin >> limit;
   fastFibonacci(limit, sequence);

   //print the sequence in a range based loop formatted with <iomanip> library
   for(auto& i : sequence){
      std::cout << std::setw(4) << std::left << num++ << " " << i << std::endl; 
   }
   return 0;
}

如果您只想打印序列中的一个数字,只需使用,例如:

std::cout << sequence[10];

而不是整个向量。

您 post 在对其他答案的评论中的代码将无法工作,因为对向量的访问在 numbers[i] = numbers[i - 1] + numbers[i - 2]; 中超出范围,例如 i = 5,您的向量只有 2 个节点,但您正在访问第 6 个节点 numbers[5].

在这个函数中

int fastFibonacci(int n) 
{
  int numbers[n+2]; // int numbers[n].
  numbers[0] = 0;
  numbers[1] = 1;
  for (int i = 2; i <= n; i++)
  {
      numbers[i] = numbers[i - 1] + numbers[i - 2];
  }
  return numbers[n];
}

使用了可变长度数组,其中 n + 2 个元素在此行中声明

  int numbers[n+2]; // int numbers[n].

变长数组不是标准的 C++ 功能。它可以作为 C++ 编译器自己的语言扩展来实现。

使用可变长度数组会使函数非常不安全,因为可能会发生堆栈溢出。

在函数中明确使用了数组的两个元素

  numbers[0] = 0;
  numbers[1] = 1;

那么即使参数的值小于 2,数组也应至少有两个元素。

要计算第 n 个斐波那契数,无需声明这样大小的数组。

除此之外,函数参数应具有无符号整数类型。否则,如果用户传递负数,该函数可能会调用未定义的行为。

对于 n 的大值,类型 int 也可能存在整数溢出。

该功能可以通过多种方式实现。

这是它的一种可能实现方式。

#include <iostream>
#include <functional>

unsigned long long fibonacci( unsigned int n )
{
    unsigned long long a[] = { 0, 1 };

    while ( n-- )
    {
        a[1] += std::exchange( a[0], a[1] );
    }

    return a[0];
}

int main() 
{
    const unsigned int N = 10;

    for ( unsigned int i = 0; i < N; i++ )
    {
        std::cout << i << ": " << fibonacci( i ) << '\n'; 
    }

    return 0;
}

程序输出为

0: 0
1: 1
2: 1
3: 2
4: 3
5: 5
6: 8
7: 13
8: 21
9: 34