厦门浪漫的餐厅:C语言程序题!!!

来源:百度文库 编辑:高考问答 时间:2024/05/01 14:22:25
Fibonacci Again

Time limit: 1 Seconds Memory limit: 32768K
Total Submit: 2100 Accepted Submit: 855

--------------------------------------------------------------------------------
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

你只需要设一个数组int f[1000000];
初始值是f[0]=1;f[1]=1;
递推式为f[n]=(f[n-1]+f[n-2])%3;
就行了

其实可以直接判断的
如下
n f[n]
0 1
1 1
2 2
3 0
4 2
5 2
6 1
7 0
8 1
9 1
注意有f[8]=f[0],f[9]=f[1]
故f是一个循环数组,对8循环

主程序部分是这样的

int n;
int f[8];
f[0]=1;
f[1]=1;
f[2]=2;
f[3]=0;
f[4]=2;
f[5]=2;
f[6]=1;
f[7]=0;
cin>>n;
if (f[n%8]==0) cout<<'Yes';
else cout<<'No';

搞定

哈哈,连ACM的题都出来了.
楼上的讲解有点小错误:f[1]=11%3=2;
其他都一样