程序日记----Fibonacci Again

来源:互联网 发布:python 3.2.5.msi下载 编辑:程序博客网 时间:2024/06/07 12:52

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

开始写的时候打算用一个数组把前面的全部装完,结果数据溢出了,看来对数据这点还不熟啊,后面参考了下网上的代码,如下

using namespace std;  int main(void)  {      int a[6] = { 1,2,0,2,2,1,0,1 };      int n;      while( cin >> n )      {          a[n%6] == 0 ? cout << "no" : cout << "yes";          cout << endl;      }  return 0;  } 

原来是要找规律,自己研究了下

n     7     110     1     0      no1     0     1      no2     1     1      yes   //从第3项开始n-2能被4整除的都符合要求3     1     2      no4     2     3      no5     3     5      no6     5     8      yes7     8     13     no8     13    21     no9     21    34     no10    34    55     yes11    55    89     no12    89    144    no...

所以给出如下代码

#include<stdio.h>int main(){    int n;    while(scanf("%d",&n)!=EOF)    {       if((n-2)%4==0)printf("yes\n");       else printf("no\n");    }    return 0;}
0 0