判断斐波那契数列中的某项是不是偶数

Description
       斐波那契数列是如下的一个数列,0,1,1,2,3,5……,其通项公式为F(n)=F(n-1)+F(n-2),(n>=2) ,其中F(0)=0,F(1)=1,你的任务很简单,判定斐波契数列的第K项是否为偶数,如果是输出YES,否则输出NO
Input
第一行,T,表示有T个测试样例。
接下来T行,每行一个数据K(0<=K<=10^10000),表示要判定的是哪一项。
Output

如果第K项是偶数,输出YES,否则输出NO。

Sample Input
2
0

1

Sample Output
YES
NO
Hint

64-bit interger is not enough for 10^10000


容易判断得出:

0

1

2

3

4

5

6

.........

所以只要判断下表能不能被3整除就可以了。而一个数能够被3整除的条件是,它的各位相加和能够被3整除。所以,输入一个string,通过判断string来判断下标能否被整除:

/*
*@author    houyong
*/
#include <iostream>
#include <cstring>
#include <cmath>
#include <ctime>
#include <vector>
#include <algorithm>
#include <iterator>
#include <queue>
#include <iomanip>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
using namespace std;
bool IsEvent(string bigInt)
{
    int sum = 0;
    for(size_t i=0; i<bigInt.size(); i++)
        sum += bigInt[i] - '0';
    return sum%3 == 0;
}


int main()
{
    int n;
    cin>>n;
    string str;
    while(n--)
    {
        cin>>str;
        cout<<(IsEvent(str)?"YES":"NO")<<endl;
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值