首页 > 其他 > 详细

(Problem 2)Even Fibonacci numbers

时间:2014-02-11 19:06:14      阅读:376      评论:0      收藏:0      [点我收藏+]

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和2开始,斐波那契数列的前十项为:

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

考虑斐波那契数列中数值不超过4百万的项,找出这些项中值为偶数的项之和。

bubuko.com,布布扣
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h>
  
#define N 4000000
  
int a[1001];
  
void solve()
{
   int a,b,c,n,count=2;
   a=1,c=0,b=2;
   n=3;
   while(c<=N)
   {
     c=a+b;
     if(n%2!=0)
     {
        a=c;
     }
     else
     {
        b=c;
     }
     n++;
     if(c%2==0)
     {
       count+=c;
     }
   }
   printf("%d",count);
}
  
int main()
{
  solve();
  return 0;
}
bubuko.com,布布扣
Answer:
4613732

(Problem 2)Even Fibonacci numbers

原文:http://www.cnblogs.com/acutus/p/3544290.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!