求用斐波那契数列( 1 1 2 3 5 8 13 21 34 55 ... n=(n-1)+(n-2))第n个数的值:
注意:如果n过大,递归次数会非常多,系统可能反应过慢
求第10个:
#include <iostream>
#include <stdio.h>
int fun(int n)//定义递归函数
{
if (n == 1 || n == 2)
return 1;
else
return fun(n - 1) + fun(n - 2);
}
int main()
{
printf("%d",fun(10));
return 0;
}
顺序输出1-10:
#include <iostream>
#include <stdio.h>
void fun(int n)//定义递归函数
{
if (n != 0)
{
fun(n - 1);
printf("%d\n", n);//输出
}
}
int main()
{
fun(10);
return 0;
}
顺序输出10-1(倒序输出1-10):
#include <iostream>
#include <stdio.h>
void fun(int n)//定义递归函数
{
if (n != 0&&n<=10)
{
fun(n +1);
printf("%d\n", n);
}
}
int main()
{
fun(1);
return 0;
}