使用两种方式实现斐波那契序列

 

/**
 * Created by wangbin on 2022/1/11.
*/

#include <stdio.h>
#include <time.h>

int fib(int n) {
    if (n <= 0) {
        return 0;
    } else if (n == 1) {
        return 1;
    } else {
        return fib(n - 1) + fib(n - 2);
    }
}

int fib2(int n) {
    int num1 = 1;
    int num2 = 1;
    int tmp = 0;
    int i;
    if (n <= 2) {
        return 1;
    } else {
        for (i = 3; i <= n ; i++) {
            tmp = num1 + num2;
            num1 = num2;
            num2 = tmp;
        }
        return tmp;
    }
}

int main(void) {
    double start, end;
    start = (double) clock();
    int res = fib2(20);
    printf("fib(20)=%d\n", res);
    end = (double) clock();
    printf("times=%.4f\n", (end - start));
    return 0;
}

 

上一篇:vue中created和mounted区别


下一篇:关于Flutter和Android混合开发的记录