题目:
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).
还有另一种Fibonacci数: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).
输入由一系列行组成,每行包含一个整数n(n<1000000)。
Output
Print the word “yes” if 3 divide evenly into F(n). Print
the word “no” if not.
如果3等分为F(n),则打印“yes”。如果没有,请打印“no”。
Sample Input
0
1
2
3
4
5
Sample Output
no
no
yes
no
no
no
解析:其实很多人第一反应会想到写个函数计算把数计算出来,但是这样会出现后面的数字出现负数的情况。这道题的数字有规律:
F(0)=7 7%3=1,不行;
F(1)=11 11%3=2,不行;
F(2)=11+7=18 18%3=0,可行;n%4=2;
F(3)=18+11=29 29%3=2,不行;
F(4)=29+18=47 47%3=2,不行;
F(5)=47+29=76 76%3=1,不行;
F(6)=76+47=123 123%3=0,可行;n%4=2;
…………
也就是说,只要输入的n%4=2就一定会被三等分。
那么代码就简单咯:
#include <stdio.h>
int main(){
int n;
while(scanf("%d",&n)!=EOF){
if(n%4==2) printf("yes\n");
else printf("no\n");
}
return 0;
}