递归

函数递归:

含义:函数自己调用自己的过程;

最简单的递归:
public static void Test1(int a)
{
//if条件很重要,就像循环中的循环条件,没有的话就永远出不来了
if (a >= 10000)
{
return;
}
a++; //这个地方也很重要,就像状态改变;
Console.WriteLine(a);
Test1(a);
}

运行机制:
一层一层往里进,到了进不了的地方,再一层一层退出来

打印菲波那切数列:
public static void Test2(int a, int b)
{
int c = a + b;

if (c > 10000)
{
return;
}

Console.WriteLine(c);

Test2(b, c);
}

BOSS题:也是面试会遇到的
写一个函数,输入一个数,返回斐波那契数列中这个数的位置的数;
0 1 1 2 3 5 8 13 21 34 55 89



原文地址:https://www.cnblogs.com/songfengyao/p/5553662.html