4.5.1简单递归消除

// DataStructTest.cpp : Defines the entry point for the console application.
//

#include 
"stdafx.h"
#include 
<iostream.h>
#include 
<malloc.h>

//递归方法
int Fib_1(int n)
{
    
if (n==0)
        
return 0;
    
else if (n==1)
        
return 1;
    
else
        
return Fib_1(n-1)+Fib_1(n-2);
}

//循环方法
int Fib_2(int n)
{
    
if (n==0)
        
return 0;
    
else
    
{
        
int x=0,y=1,z=0;
        
for(int i=2;i<=n;i++)
        
{
            z
=y;
            y
=x+y;
            x
=z;
        }

        
return y;
    }

}


int main(int argc, char* argv[])
{
    cout
<<"递方方法计算N=20的斐波那齐数列"<<Fib_1(20)<<endl;
    
//
    cout<<"循环方法计算N=20的斐波那齐数列"<<Fib_2(20)<<endl;
    
//
    return 0;
}

posted @ 2007-07-12 16:17  吴东雷  阅读(245)  评论(0编辑  收藏  举报