HDU 1021Fibonacci Again

来源:互联网 发布:热血传奇蛮王数据 编辑:程序博客网 时间:2024/06/10 04:58

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

012345
 

Sample Output

nonoyesnonono
由于n很大,直接递归会栈溢出,如下:
#include<stdio.h>int f(int n){int i;if(n==0)return 7;else if(n==1)return 11;elsereturn f(n-1)+f(n-2);}int main(){int n;while(scanf("%d",&n)!=EOF){if(f(n)%3==0)printf("yes\n");elseprintf("no\n");}return 0;}
只有找规律,除了n=0、1、2以外,每3个no就一个yes
#include<stdio.h>int main(){int n;while(scanf("%d",&n)!=EOF){if(n<3)printf(n==2?"yes\n":"no\n");elseprintf((n-2)%4==0?"yes\n":"no\n");}return 0;}

0 0
原创粉丝点击