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

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

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

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <math.h> #define N 4000000 int a[]; void solve()
{
int a,b,c,n,count=;
a=,c=,b=;
n=;
while(c<=N)
{
c=a+b;
if(n%!=)
{
a=c;
}
else
{
b=c;
}
n++;
if(c%==)
{
count+=c;
}
}
printf("%d",count);
} int main()
{
solve();
return ;
}
Answer:
4613732
上一篇:yum的使用及配置


下一篇:[转]装完CentOS后,重新开机启动后显示: Initial setup of CentOS Linux 7 (core)