标签:
#include <stdio.h> int fib(int n) { int a = 1; int b = 1; int c = a = b; int i = 0; for (i = 3; i <= n; i++) { c = a + b; a = b; b = c; } return c; } int main() { printf("%d\n", fib(10000)); getchar(); return 0; }
非递归方法
#include <stdio.h> int fib(int n) { int count = 0; if (n <= 2) return 1; else if (n == 4) count++; else return fib(n - 2) + fib(n - 1); } int main() { printf("%d\n", fib(10000)); getchar(); return 0; }
总结:递归的效率较循环等方法,效率较低,尤其数值较大时,效率极慢,且易造成栈溢出。
标签:
原文地址:http://blog.csdn.net/lolita0917/article/details/45621871