hdu1021 Fibonacci Again

来源:互联网 发布:linux 关闭swap分区 编辑:程序博客网 时间:2024/06/11 04:54

Fibonacci Again

 

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

Sample Output

no

no

yes

no

no

no

Author

Leojay

 

 

先看这道题是很简单的斐波那契数列,输出是否是三的倍数

斐波那契数列用递归和循环都可以实现

递归:

#include<stdio.h>

 

int main()

{

    int f(int );

    int n,result;

    while(1)

    {

        scanf("%d",&n);

        result=f(n);

        printf("%d\n",result);

        if(result%3==0)

            printf("yes\n");

        else

            printf("no\n");

    }

    return 0;

}

 

int f(int n)

{

    if(n==0)

        return 7;

    else if(n==1)

        return 11;

    else

        return f(n-1)+f(n-2);

}                    //用递归要调用函数费时,在做ACM时对运行时间是有要求的,所以应避免调用函数;

循环:

#include <stdio.h>

 

int main()

{

    int n;

    while(scanf("%d",&n)!=EOF)

    {

        int t,x=7,y=11;

        if(n==0)

            t=x;

        else if(n==1)

            t=y;

        else

        {

            for(int i=1;i<n;i++)

            {

                t=x+y;

                x=y;

                y=t;

            }

        }

        if(y%3==0)

            printf("yes\n",n);

        else

            printf("no\n");

    }

    return 0;

}

这样的程序都可以简单地实现题目所要求的功能

但原题中提到“(n < 1,000,000).”所以斐波那契数列的数一定会超过int的范围,出现溢出问题,大概在n大于50以后以上程序便无法处理;

优化:考虑到斐波那契数列的数时相加求得的,我们要的结果时模3的结果,因此我们为避免出现超大的数我发处理,可以用斐波那契数列中数模三的余数来代替原数,这样达到的效果是一样的。如下:

正解:

#include <stdio.h>

 

int main()

{

    int n;

    while(scanf("%d",&n)!=EOF)

    {

        int t,x=7,y=11;

        if(n==0)

            t=x;

        else if(n==1)

            t=y;

        else

        {

            for(int i=1;i<n;i++)

            {

                t=x%3+y%3;

                x=y;

                y=t;

            }

        }

        if(y%3==0)

            printf("yes\n");

        else

            printf("no\n");

    }

    return 0;

}

 

Water。。。都说这是一套水题了,而这一道题有是道特别的水题,因为它是水题中的水题;

介于考虑到以上正确代码有29行;作为一道水题提交这么多行代码,不是很甘心啊!

然后运行了一下以下代码:

#include <stdio.h>

 

int main()

{

    int n;

    for(n=0;n<100;n++)

    {

        int t,x=7,y=11;

        if(n==0)

            t=x;

        else if(n==1)

            t=y;

        else

        {

            for(int i=1;i<n;i++)

            {

                t=x%3+y%3;ju

                x=y;

                y=t;

            }

        }

        if(y%3==0)

            printf("yes%d\n",n);

        else

            printf("no  ");

    }

    return 0;

}

 

 

 

发现了什么?!直接把代码改成:

正解:

#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;

}

 

 

就这么简单。

 

原创粉丝点击