[1021]:Fibonacci Again

来源:互联网 发布:2016最新人口普查数据 编辑:程序博客网 时间:2024/06/06 12:46

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题目,我抽出一个可以用的模板:

0 0
原创粉丝点击