c++ - 斐波那契数列通过串行代码

标签 c++ fibonacci

我想知道当我输入 n=40 时,执行以下斐波那契序列代码实际需要多少时间???

#include<stdio.h>

void printFibonacci(int);

int main(){

    int k,n;
    long int i=0,j=1,f;

    printf("Enter the range of the Fibonacci series: ");
    scanf("%d",&n);

    printf("Fibonacci Series: ");
    printf("%d %d ",0,1);
    printFibonacci(n);

    return 0;
}

void printFibonacci(int n){

    static long int first=0,second=1,sum;

    if(n>0){
         sum = first + second;
         first = second;
         second = sum;
         printf("%ld ",sum);
         printFibonacci(n-1);
    }

}

最佳答案

在 Linux 上,您可以使用“time”命令测量程序的执行时间:

在 Windows 上,您可以从 Windows 资源工具包下载“timer.exe”,或使用以下“技巧”之一:

关于c++ - 斐波那契数列通过串行代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13946433/

相关文章:

c++ - 找不到 Visual Studio 2008 的构建工具(平台工具集 = 'v90' )

c++ - 英特尔++ : warning #1563: taking the address of a temporary - false positive?

c++ - 读取和修改文件的最佳方式 (c++)

python - 如何在 Python 中将斐波那契数列打印到第 n 个数?

python - 单语句斐波那契

c++ - 有没有办法转发声明协方差?

c++ - C++ 二维 vector 中的迭代

recursion - OCaml 中的内存?

java - 斐波那契数列错误

c++ - 在 C++11 中的编译时 (constexpr) 计算斐波那契数(递归方法)