入门训练 Fibonacci数列

http://lx.lanqiao.cn/problem.page?gpid=T4

#include <iostream>
using namespace std;

int main()
{
    int Fib_Front = 1, Fib_Rear = 1, Number, Answer;
    cin >> Number;
    for(int i = 3; i <= Number; i+=2)
    {
        Fib_Front = ((Fib_Front + Fib_Rear) > 10007)? (Fib_Front +Fib_Rear) % 10007 : (Fib_Front + Fib_Rear);
        Fib_Rear = ((Fib_Front + Fib_Rear) > 10007)? (Fib_Front +Fib_Rear) % 10007 : (Fib_Front + Fib_Rear);
    }
    Answer = (Number % 2 != 0)? Fib_Front:Fib_Rear;
    cout << Answer << endl;
    return 0;
}
#include <iostream>
using namespace std;
long long Arr[1000000];

void Fibonacci()
{
    Arr[0] = 0;
    Arr[1] = Arr[2] = 1;
    for(int i = 3; i < 1000000; i++)
    {
        Arr[i] = (Arr[i - 1] + Arr[i - 2])%10007;
    }
}
int main()
{
    int Number;
    Fibonacci();
    cin >> Number;
    cout << Arr[Number] << endl;
    return 0;
}

 

posted on 2018-09-23 11:09  Misko  阅读(255)  评论(0编辑  收藏  举报