(Problem 2)Even Fibonacci numbers

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <ctype.h>
 4 #include <math.h>
 5   
 6 #define N 4000000
 7   
 8 int a[1001];
 9   
10 void solve()
11 {
12    int a,b,c,n,count=2;
13    a=1,c=0,b=2;
14    n=3;
15    while(c<=N)
16    {
17      c=a+b;
18      if(n%2!=0)
19      {
20         a=c;
21      }
22      else
23      {
24         b=c;
25      }
26      n++;
27      if(c%2==0)
28      {
29        count+=c;
30      }
31    }
32    printf("%d",count);
33 }
34   
35 int main()
36 {
37   solve();
38   getchar();
39   getchar();
40   return 0;
41 }

 

Answer:
4613732

 

posted @ 2013-07-26 10:57  cpoint  阅读(265)  评论(0编辑  收藏  举报
浏览次数:travelocity promotion codes