1021_FibonacciAgain

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.

ACM的题目,写出递归,虽然只有两句,不过心里很舒服。

View Code
1 #include<stdio.h>
 2 #include<stdlib.h>
 3 typedef struct node{
 4     int x;
 5     struct node *next;
 6 }myNode;
 7 int main()
 8 {
 9     myNode *head,*p;
10     int Num,n;
11     void creat(myNode *head,int n);
12     int calculate(int a);
13     head=(myNode*)malloc(sizeof(myNode));
14     head->next=NULL;
15     printf("input the Num:\n");
16     scanf("%d",&n);
17     creat(head,n);
18     p=head->next;
19     while(p!=NULL)
20     {
21         Num=calculate(p->x);
22         if(Num%3==0)
23             printf("yes\n");
24         else
25             printf("no\n");
26         p=p->next;
27     }
28     return 0;
29 }
30 
31 void creat(myNode *head,int n)
32 {
33     myNode *p1,*p2;
34     int i;
35     p2=head;
36     for(i=1;i<=n;i++)
37     {
38         p1=(myNode*)malloc(sizeof(myNode));
39         scanf("%d",&p1->x);
40         p1->next=p2->next;
41         p2->next=p1;
42         p2=p1;
43     }
44 }
45 
46 int calculate(int a)
47 {
48     int count=0;
49     if(a==0)
50         return 7;
51     else if(a==1)
52         return 11;
53     else
54     {
55         count+=calculate(a-1);
56         count+=calculate(a-2);
57     }
58     return count;
59 }
posted @ 2012-09-27 11:50  长溪  阅读(134)  评论(0编辑  收藏  举报