0001 #include<time.h> 0002 #include<iostream> 0003 using namespace std; 0004 0005 #include "Fib.h" 0006 0007 __int64 fibI ( int n ); //迭代版 0008 __int64 fib ( int n ); //二分递归版 0009 __int64 fib ( int n, __int64& f ); //线性递归版 0010 0011 int main ( int argc, char* argv[] ) { //测试FIB 0012 // 检查参数 0013 if ( 2 > argc ) { fprintf ( stderr, "Usage: %s <rank>\n", argv[0] ); return 1; } 0014 int n = atoi ( argv[1] ); 0015 // 依次计算Fibonacci数列各项 0016 printf ( "\n------------- class Fib -------------\n" ); 0017 Fib f ( 0 ); 0018 for ( int i = 0; i < n; i++, f.next() ) 0019 printf ( "fib(%2d) = %d\n", i, f.get() ); 0020 for ( int i = 0; i <= n; i++, f.prev() ) 0021 printf ( "fib(%2d) = %d\n", n - i, f.get() ); 0022 printf ( "\n------------- Iteration -------------\n" ); 0023 for ( int i = 0; i < n; i++ ) 0024 printf ( "fib(%2d) = %22I64d\n", i, fibI ( i ) ); 0025 printf ( "\n------------- Linear Recursion -------------\n" ); 0026 for ( int i = 0; i < n; i++ ) { 0027 __int64 f; 0028 printf ( "fib(%2d) = %22I64d\n", i, fib ( i, f ) ); 0029 } 0030 printf ( "\n------------- Binary Recursion -------------\n" ); 0031 for ( int i = 0; i < n; i++ ) 0032 printf ( "fib(%2d) = %22I64d\n", i, fib ( i ) ); 0033 return 0; 0034 }