码迷,mamicode.com
首页 > 其他好文 > 详细

入门训练 Fibonacci数列

时间:2018-01-31 14:18:00      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:head   ade   std   pac   stream   end   cin   header   clu   

每计算一个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数列

标签:head   ade   std   pac   stream   end   cin   header   clu   

原文地址:https://www.cnblogs.com/denghui666/p/8391250.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!