斐波那契数

来源:互联网 发布:nginx访问日志格式 编辑:程序博客网 时间:2024/05/17 22:03
#define _CRT_SECURE_NO_WARNINGS 1#include<stdio.h>//递归法求斐波那契数(图1)long fibonacci(int n){if (n <= 2)return 1;return fibonacci(n - 1) + fibonacci(n - 2);}//迭代法求斐波那契数(图2)long fibonacci(int n){long Pre;long Cur;long Result;Pre = 1;Cur = 1;for (int i = 3; i <= n; i++){Result = Pre + Cur;Pre = Cur;Cur = Result;}return Result;}//迭代法求斐波那契数(图3)long fibonacci(int n){long result;long previous_result;long next_older_result;result = previous_result = 1;while (n > 2){n--;next_older_result = previous_result;previous_result = result;result = previous_result + next_older_result;}return result;}

原创粉丝点击