nrfib.cpp

来自「数据结构c++语言描述 Borland C++实现」· C++ 代码 · 共 42 行

CPP
42
字号


// nonrecursive code to compute a Fibonacci number

#include <iostream.h>

int Fibonacci(int n)
{// Return the n'th Fibonacci number, n >= 0

   if (n == 0) return 0;
   if (n == 1) return 1;
   int SecondLast = 0;  // second last Fibonacci number
                        // computed so far
   int Last = 1;        // last Fibonacci number
                        // computed so far
   // compute new numbers
   for (int i = 2; i <= n; i++) {
      // compute i'th Fibonacci number
      int Next = Last + SecondLast;
      // update Last and SecondLast
      SecondLast = Last;
      Last = Next;
      }

   return Last;
}

void main(void)
{
   cout << "The 2nd Fibonacci number is "
        << Fibonacci(2) << endl;

   cout << "The 3rd Fibonacci number is "
        << Fibonacci(3) << endl;

   cout << "The 4th Fibonacci number is "
        << Fibonacci(4) << endl;

   cout << "The 5th Fibonacci number is "
        << Fibonacci(5) << endl;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?