Fibonacci数列

递归

static int GetValue(int n)
{
    if (n <= 0)
        return 0;
    if (n == 1 || n == 2)
        return 1;
    else
        return GetValue(n - 1) + GetValue(n - 2);
}

迭代

static int GetValue2(int n)
{
    if (n <= 0)
        return 0;
    if (n == 1 || n == 2)
        return 1;
    int first = 1, second = 1, third = 0;
    for (int i = 3; i <= n; i++)
    {
        third = first + second;
        first = second;
        second = third;
    }
    return third;
}
把圈子变小,把语言变干净,把成绩往上提,把故事往心里收,现在想要的以后你都会有。
原文地址:https://www.cnblogs.com/jizhiqiliao/p/15623748.html