递归函数:
#include<stdio.h>
#include<stdlib.h>
int Fib(int a) {
if (a <= 2) {
return 1;
}
else {
return Fib(a - 2) + Fib(a - 1);
}
}
int main() {
printf("%d\n", Fib(6));
system("pause");
return 0;
}
输出第六个斐波那契数列:非递归:
#include<stdio.h>
#include<stdlib.h>
int main() {
int a = 1;
int b = 1;
int tmp;
int i;
for (i = 3; i <= 6; i++) {
tmp = a + b;
a = b;
b = tmp;
}
printf("%d\n",tmp);
system("pause");
}
运行结果: