再帰的形式と非再帰的形式のフィボナッチ数列の用法分析



<SPAN style="FONT-SIZE: 32px"> </SPAN>

#include "stdafx.h"
#include <iostream>
using namespace std;
//
int fibonacciRecursion(int n)
{
 if (n == 1 || n ==2)
 {
  return 1;
 }
 if (n > 2)
 {
  return fibonacciRecursion(n - 1) + fibonacciRecursion(n - 2);
 }
}
//
//
//
int fibonacci(int n)
{
 int temp[2];
 temp[0] = 1;
 temp[1] = 1;
 if (n == 1 || n == 2)
 {
  return 1;
 }
 else
 {
  for (int i = 2; i < n; i ++)
  {
   int tp = temp[0] + temp[1];
   temp[1] = temp[0];
   temp[0] = tp;
  }
  return temp[0];
 }
}
試験コード:

int _tmain(int argc, _TCHAR* argv[])
{
 cout << fibonacci(1) << " " << fibonacci(2) << " " << fibonacci(3) << " " << fibonacci(4) << " "
 << fibonacci(5) << " " << fibonacci(6) << " "<< fibonacci(7) << " "<< fibonacci(8) << " "
 << fibonacci(9) << " " << fibonacci(10) << endl;
 cout << fibonacciRecursion(1) << " " << fibonacciRecursion(2) << " " << fibonacciRecursion(3) << " " <<
  fibonacciRecursion(4) << " "<< fibonacciRecursion(5) << " " << fibonacciRecursion(6) << " "<< fibonacciRecursion(7)
  << " "<< fibonacciRecursion(8) << " "<< fibonacciRecursion(9) << " " << fibonacciRecursion(10) << endl;
 return 0;
}