HDOJ 5018 Revenge of Fibonacci

来源:互联网 发布:基本法知乎 编辑:程序博客网 时间:2024/05/09 02:45

题意:输入斐波那契数列最初的两个值,要求判断输入的n是否存在于该数列中。

链接:http://acm.hdu.edu.cn/showproblem.php?pid=5018

解题思路:将每组第k个数据递推至大于等于n时进行判断,当num[k]==n时,n存在于数列中,否则n不存在。

注意点:n可能为最初始的两个值。

AC代码如下:

Run IDSubmit TimeJudge StatusPro.IDExe.TimeExe.MemoryCode Len.LanguageAuthor116875112014-09-19 22:20:37Accepted501815MS364K679 BG++

#include <iostream>#include <cstdio>#include <algorithm>using namespace std;int main(){    int t;    cin >> t;    while ( t -- )    {        long long a, b, c;        cin >> a >> b >> c;        long long tmp = a + b;        if ( a == c || b == c )        {            tmp = c;        }        else        {            while ( tmp < c )            {                a = b;                b = tmp;                tmp = a + b;            }        }        if ( tmp == c )        {            cout << "Yes" << endl;        }        else        {            cout << "No" << endl;        }    }    return 0;}


0 0