1. 程式人生 > >入門訓練 Fibonacci數列

入門訓練 Fibonacci數列

namespace pre acc clu style nac iostream fib cout

每計算一個Fi,都對其取余,防止最後其超出int的範圍

#include<iostream>
using namespace std;
int main()
{
    int n;
    while(cin >> n)
    {
        int x = 1;
        int y = 1;
        int z;
        if(n < 3)
          cout << 1 << endl;
        else
        {
            for(int i = 3; i <= n; i++)
            {
                z 
= x + y; z = z % 10007; x = y; y = z; } cout << z << endl; } } }

入門訓練 Fibonacci數列