您的位置:首页 > 大数据 > 人工智能

Fibonacci Again解题报告

2013-07-29 19:41 183 查看
题目摘要:Thereare another kind of Fibonacci numbers: F(0) = 7, F(1) = 11, F(n) = F(n-1) +F(n-2) (n>=2).
题目大意:另外一种斐波那契数列,第一项为7,第二项为11。

输入输出要求

Input

Input consists of a sequence of lines, eachcontaining an integer n. (n < 1,000,000).

 

Output

Print the word "yes" if 3 divideevenly into F(n).

Print the word "no" if not.

 

输入输出样例

Sample Input

0

1

2

3

4

5

 

Sample Output

no

no

yes

no

no

no

 

解题思路:咳咳,找规律,8个一循环。

代码

#include<iostream>

using namespace std;

long long f(int n)

{

    long long a=7;

    long long b=11;

    long long c;

    int i;

    if(n==0)

        return 7;

    else if(n==1)

        return 11;

    else

    {

        for(i=2;i<=n;i++)

        {

           
c=a+b;

           
a=b;

           
b=c;

        }

    }

    return b;

}

int main()

{

    int n;

    while(cin>>n)

    {

        n%=8;

        
if(f(n)%3==0)

           
cout<<"yes"<<endl;

        else

           
cout<<"no"<<endl;

    }

    return 0;

}

解题感想:最先想到直接用递归求数列,结果发现n能取100万,递归肯定递不出来,所以用了另一种方法求f(n),开一个循环就好。交了代码WA掉,怎么也没想明白错在哪,后来将f(n)输出看看,发现n去90以后结果就变负值了,原因是long long也不够存f(n)的值。这样的话只能再换思路了,因为题中是对3取余,所以发现结果是8个一循环,将n对8取余一下就好了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  解题报告