Μῆτις -- chengfengYang_LMN

enjoy programming (๑•ᴗ•๑) status:百度(实习)

[1021]:Fibonacci Again

Fibonacci Again

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 46300 Accepted Submission(s): 22077

Problem Description
There are another kind of Fibonacci numbers: F(0) = 7, F(1) = 11, F(n) = F(n-1) + F(n-2) (n>=2).

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

Output
Print the word “yes” if 3 divide evenly 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

解题思路:刚开始看,这道题好像很容易,不就是求能否整除3吗?那就找一下规律(如:图一)不过会让你出错的地方是:(n < 1,000,000).,由于给出的n的范围偏大,这可能会是F(n)达到一定程度而溢出
这里写图片描述

#include<stdio.h>
#define F0 7
#define F1 11//题意给出的F(0) F(1)

int num;
int main()
{
    while(scanf("%d", &num)!=EOF){
        int a = F0%3, b = F1%3, n=2;
        if(num==0 || num==1){
            printf("no\n");
        }
        else{
            for(; n<=num; n++){
            int left = (a+b)%3;//①这一部分解释如图一
            a = b;
            b = left;
            if(left%3==0 && n==num){
                printf("yes\n");
            }
            else if(left%3!=0 && n==num){
                printf("no\n");
            }
            }
        }
    }
    return 0;
}

这里写图片描述

如果有类似的Fibonacci Again题目,我抽出一个可以用的模板:

阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yang_chengfeng/article/details/49926313
文章标签: acm-FiAgai
个人分类: 剑指ACM
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

[1021]:Fibonacci Again

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭