斐波那契数列的高性能实现

bool fibon_elem(int pos, unsigned int &res)

{

    if (pos <= 0 || pos > 1024)

        return false;



    res = 1;

    unsigned int f_1 = 1;

    unsigned int f_2 = 1;

    for (int index = 3; index <= pos; ++index)

    {

        res = f_1 + f_2;

        f_1 = f_2;

        f_2 = res;

    }

    return true;

}



bool fib_quene(int pos, unsigned int &res)

{

    if (pos<0 || pos>1024)

        return false;

    queue <unsigned int > temp;

    temp.push(1);

    temp.push(1);

    for (int index = 3; index <= pos; ++index)

    {

        temp.push(temp.front()+temp.back());

        temp.pop();

    }

    res = temp.back();

    return true;

}

 

你可能感兴趣的:(高性能)