C语言实现斐波那契数列
1.函数一用递归实现
2.函数二用循环实现
#include<stdio.h> #include<stdlib.h> #pragma warning(disable:4996) //斐波那契 1,1,2,3,5,8,13... int fibo1(int n) { if (n == 1 || n==2) { return 1; } return fibo1(n-2)+fibo1(n - 1); } void fibo2(int n) { if (n == 1 || n == 2) { return 1; } int f1 = 1; int f2 = 1; int fn = 0; for (int i = 1; i <= n-2; i++) { fn = f1 + f2; f1 = f2; f2 = fn; } printf("%d\n", fn); } int main(){ printf("%d\n", fibo1(5)); fibo2(5); system("pause"); }