Fibonacci Again
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other)
Total Submission(s) : 49 Accepted Submission(s) : 19
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
0
1
2
3
4
5
Sample Output
no
no
yes
no
no
no
计算过程如下:
斐波那契数列是一项的值等于前两项之和,因此可以很容易的写出计算这个数列值的代码,本道题里斐波那契数列第一二项为7和11,所以先观察数列规律,用3取余为零的规律。
计算本题中斐波那契数列的n项是否为3的倍数的代码如下(本代码不适用与题目):
#include <stdio.h>
int main()
{
int a[100], i, n = 0;
while (scanf("%d",&n)!=EOF)
{
a[1] = 11, a[0] = 7;
for (i = 2; i < 100; i++)
a[i] = a[i - 2] + a[i - 1];
if (a[n] % 3 != 0)
printf("no\n");
else
printf("yes\n");
}
return 0;
}
因此也就是输入的n%4=2的话这个项便是题目符合项,所以按规律算的话最后的代码还是很简单的:
#include <stdio.h>
int main()
{
int n = 0;
while (scanf("%d",&n)!=EOF)
{
if (n % 4 != 2)
printf("no\n");
else
printf("yes\n");
}
return 0;
}