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;
}