永发信息网

杭电1021题,为么事我写的不能通过,求纠正

答案:2  悬赏:20  手机版
解决时间 2021-02-25 01:56
  • 提问者网友:未信
  • 2021-02-24 18:15
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).
#include "stdio.h"
#include"stdlib.h"
main()
{

long n,i,f1,f2,f3;
while((scanf("%d",&n)!=EOF)&&n<1000000)
{
f1=1;
f2=2;
if(n==0||n==1)
f3=1;
else
for(i=1;i {
f3=(f1+f2)%3;
f1=f2;
f2=f3;
}
if(f3==0)
printf("yes\n");
else
printf("no\n");
}
system("pause");
}

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
最佳答案
  • 五星知识达人网友:玩家
  • 2021-02-24 19:11
找规律,不能硬算
F(0)= 1, F(1) = 2, F(n) = ( F(n-1) + F(n-2) )( mod 3) (n>=2).

index  0  1  2  3  4  5  6  7  8  9  10  11  12  13

value  1  2  0  2  2  1  0  1  1  2   0   2   2  1

print  no no yes no  no no yes  no  no  no  yes  no  no  no

这样我们就得到了如下规律:从第2个开始每隔4个循环一次。

#include
int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
if((n-2)%4!=0)
printf("no\n");
else
printf("yes\n");
}
return 0;
}
全部回答
  • 1楼网友:空山清雨
  • 2021-02-24 20:06
根据题意:n=0时,f3=7,n=2时,f3=11,所以你写的不正确,还有f3在计算斐波那契数列是不能取除3的余数,不然算出的数列不是斐波那契数列,所以应该这么写: #include "stdio.h" #include"stdlib.h" main() { long n,i,f1,f2,f3; while((scanf("%d",&n)
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯