标签:斐波那契数 oid isp ++ closed pre splay lse system
/*
斐波那契数列:f(n)=f(n-1)+f(n-2);其中f(1)=f(2)=1;
*/
#include <stdio.h> #include <stdlib.h> /* 斐波那契数列:f(n)=f(n-1)+f(n-2);其中f(1)=f(2)=1; */ int Fibonacci(int n) { if (n == 1 || n == 2) return 1; else return Fibonacci(n - 1) + Fibonacci(n - 2); } void main() { printf("%d \n", Fibonacci(40)); // 102334155 ,递归调用 5s (利用内存的堆栈) system("pause"); }
#include <stdio.h> #include <stdlib.h> void main() { int a[40] = { 0 }; a[0] = 1; a[1] = 1; for (int i = 2; i < 40; i++) { a[i] = a[i - 1] + a[i - 2]; } printf("%d \n", a[39]); system("pause"); }
#include <stdio.h> #include <stdlib.h> void main() { int f1 = 1, f2 = 1, f3; // 与数组类似 0s for (int i = 1; i < 40 - 1; i++) { f3 = f1 + f2; f1 = f2; f2 = f3; printf("%-10d ", f3); if (i % 5 == 0) puts("\n"); }*/ system("pause"); }
标签:斐波那契数 oid isp ++ closed pre splay lse system
原文地址:https://www.cnblogs.com/ant-colonies/p/13334575.html