⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fibtest.cpp

📁 C++&datastructure书籍源码,以前外教提供现在与大家共享
💻 CPP
字号:
#include <iostream>using namespace std;#include "ctimer.h"#include "prompt.h"// Illustrates "bad" recursion for computing Fibonacci numbersconst int FIB_LIMIT = 20;            // largest fib # calculatedlong RecFib(int n)// precondition: 0 <= n// postcondition: returns the n-th Fibonacci number{    if (0 == n || 1 == n)    {   return 1;    }    else    {   return RecFib(n-1) + RecFib(n-2);    }}long Fib(int n)// precondition: 0 <= n// postcondition: returns the n-th Fibonacci number{    long first=1, second=1, temp;    int k;    for(k=0; k < n; k++)    {   temp = first;        first = second;        second = temp + second;    }    return first;}int main(){    CTimer rtimer,itimer;	int j;	long  k;    long ival,rval;	long iters = PromptRange("enter # of iterations",1L,100000L);	int limit =  PromptRange("n, for n-th Fibonacci ",1,30);        for(k = 0; k < iters; k++)    {   for(j=0; j <= limit; j++)        {   rtimer.Start();            rval = RecFib(j);            rtimer.Stop();            itimer.Start();            ival = Fib(j);            itimer.Stop();            if (ival != rval)            {   cout << "calls differ for " << j << endl;                cout << "recursive = " << ival << " iterative = " << rval << endl;            }        }    }    cout << iters << " recursive trials " << rtimer.CumulativeTime() << endl;    cout << iters << " iterative trials " << itimer.CumulativeTime() << endl;    return 0;}

⌨️ 快捷键说明

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